Click here to Skip to main content
15,935,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I am newbie to linq expression so can anyone help with my problem?Let me explain, I have written a lambda expression for selecting all the details of function between a particular date and linq is :
db.Meals.Where(c => c.MealDate== date && c.SessionID == SessionID && c.MealType == MealType ).FirstOrDefault();

see problem in my db the mealdate has both date and time and my date variable is only having date ,so I want to take only the date portion and neglect the time part.Any ideas,will be a great help to me.
Updated 13-Nov-12 23:19pm

It seems Solution 3 still may have edge cases that don't match.
Just compare the .Date property on the DateTime values:
CampCuisineDataContext db = new CampCuisineDataContext();
return db.Meals.Where(c => c.MealDate.Value.Date == date.Date &&
                           c.SessionID == SessionID &&
                           c.MealType == MealType).FirstOrDefault();

Also, your use of .Value on c.MealDate seems to imply that the MealDate column is nullable and the expression needs to check that it actually has a value.
Share this answer
Mani Zachariah 14-Nov-12 22:36pm    
ok will check...thanks for helping Matt T Heffron
sorry a slight change...
CampCuisineDataContext db = new CampCuisineDataContext();
 return db.Meals.Where(c => (c.MealDate.Value - date).Days == 0 && (c.MealDate.Value - date).Hours > 0 && c.SessionID == SessionID && c.MealType == MealType).FirstOrDefault();

dis the final ans..
Thanks all
Share this answer
Nelek 14-Nov-12 16:27pm    
Next time don't add a new solution. You can edit your previous messages if you click on the widget "improve question" / "improve solution". I delete the false one to avoid missunderstoods
Mani Zachariah 14-Nov-12 22:36pm    
Sorry Nelek!!!I'm new to code project so dint know abt editing thing.

Please formate the date according to your need.

modify your statement as below.

db.Meals.Where(c => String.Format("{0:M/d/yyyy}", c.MealDate) == date && c.SessionID == SessionID && c.MealType == MealType ).FirstOrDefault();

Refer below links for formatting date in particular formate.[^][^][^]
Share this answer
Mani Zachariah 14-Nov-12 5:01am    
Hi Mohd.Mukhtar,
Thanks for replying,but unfortunateky it dint work showing an error message like:
No overload for method 'ToString' takes 1 arguments
Can we use datetime.parse?
Mani Zachariah 14-Nov-12 5:12am    
Any equivalent expression for ling:
select convert(varchar(10),'7/22/2012 12:00:00 AM',1) as [MM/DD/YYYY]
Mani Zachariah 14-Nov-12 5:13am    
If there is then it will solve my problem.Please help?
Mohd. Mukhtar 14-Nov-12 5:32am    
see the updated ans and refer given links for more details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900