Seevali H Rathnayake
Seevali H Rathnayake

Reputation: 595

EF5 + Using the 'LIKE' operator inside a Lambda Expression

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.

db.Sales.OrderByDescending(O => O.Date).Where(O => O.Date == DateTime.Now ).Select(O => O.TrNo).FirstOrDefault();

Please help me to get the build the right code. (My db is MySQL)

Thanks!

Upvotes: 0

Views: 472

Answers (2)

Tom Styles
Tom Styles

Reputation: 1082

If you only want to test the date portion of the datetime try this.

db.Sales.OrderByDescending(O => O.Date).Where(O => O.Date == DateTime.Now.Date ).Select(O => O.TrNo).FirstOrDefault();

Upvotes: 0

eandersson
eandersson

Reputation: 26352

If O.Date is a Date, simply compare it against DateTime.Now.Date, since DateTime.Now will return both the Date & Time. If O.Date is actually a DateTime, simply do the same, O.Date.Date == DateTime.Now.Date.

Upvotes: 2

Related Questions