In my Entity (Sale) has a type of DateTime column Date.
I used following lambda expression to get the latest TrNo from that table. But always it gives me Null. Because it also compares the
Time part of the column. Actually I wanted to compare the
Date part of the column.
Firstly, you will need to check in your Database that you actually have data that this expression will match.
To compare the Date property of a DateTime object, just use DateTime.Date :
db.Sales.Where(d => d.Date.Date == DateTime.Now.Date) //Checks that the Dates are Equal
.OrderByDescending(ob => ob.Date) //Orders by the Date
.FirstOrDefault().TrNo; //Gets the First TrNo
The above statement assumes that your entity resembles something like this :
public class Sale
{
public DateTime Date { get; set; }
public string TrNo { get; set; }
}
Seevali
0 Points
11 Posts
EF5 + Using the "LIKE" operator inside a Lambda Expression
Feb 01, 2013 10:58 AM|LINK
In my Entity (Sale) has a type of DateTime column Date.
I used following lambda expression to get the latest TrNo from that table. But always it gives me Null. Because it also compares the Time part of the column. Actually I wanted to compare the Date part of the column.
Please help me to get the build the right code. (My db is MySQL)
Thanks!
Rion William...
All-Star
26554 Points
4414 Posts
Re: EF5 + Using the "LIKE" operator inside a Lambda Expression
Feb 01, 2013 11:08 AM|LINK
Firstly, you will need to check in your Database that you actually have data that this expression will match.
To compare the Date property of a DateTime object, just use DateTime.Date :
db.Sales.Where(d => d.Date.Date == DateTime.Now.Date) //Checks that the Dates are Equal .OrderByDescending(ob => ob.Date) //Orders by the Date .FirstOrDefault().TrNo; //Gets the First TrNoThe above statement assumes that your entity resembles something like this :
public class Sale { public DateTime Date { get; set; } public string TrNo { get; set; } }