Click here to Skip to main content
15,885,546 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 :
C#
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.
Posted
Updated 13-Nov-12 23:19pm
v2

Hi,

Please formate the date according to your need.

modify your statement as below.

C#
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.

http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^]

http://www.mikesdotnetting.com/Article/23/Date-Formatting-in-CSharp[^]

http://www.csharp-examples.net/string-format-datetime/[^]
 
Share this answer
 
v6
Comments
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.
sorry a slight change...
SQL
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
 
Comments
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.
It seems Solution 3 still may have edge cases that don't match.
Just compare the .Date property on the DateTime values:
C#
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
 
Comments
Mani Zachariah 14-Nov-12 22:36pm    
ok will check...thanks for helping Matt T Heffron

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