Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
Posted 13-Nov-12 23:47pm
Edited 14-Nov-12 0:19am
digimanus30.8K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
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.
 
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/[^]
  Permalink  
v6
Comments
Mani Zachariah at 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 at 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 at 14-Nov-12 5:13am
   
If there is then it will solve my problem.Please help?
Mohd. Mukhtar at 14-Nov-12 5:32am
   
see the updated ans and refer given links for more details.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Nelek at 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 at 14-Nov-12 22:36pm
   
Sorry Nelek!!!I'm new to code project so dint know abt editing thing.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
Mani Zachariah at 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)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 195
3 DamithSL 195
4 Richard MacCutchan 178
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100