Linq用于选择使用Lambda表达式 [英] Linq for selecting using lambda expression

查看:147
本文介绍了Linq用于选择使用Lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是linq表达式的新手,所以有人可以解决我的问题吗?让我解释一下,我编写了一个lambda表达式,用于选择特定日期和linq之间的函数的所有详细信息是:

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();



在我的数据库中看到问题,饭食同时具有日期和时间,并且我的date变量仅具有date,所以我只想选择日期部分而忽略时间部分.任何想法对我都会有很大的帮助.



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.

推荐答案



请根据您的需要安排日期.

如下修改您的声明.

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();




请参阅下面的链接以格式化日期,尤其是格式.

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/ [ ^ ]




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/[^]


对不起,稍有变化...
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最后的答案..
谢谢所有


dis the final ans..
Thanks all


似乎解决方案3仍然可能存在边缘情况不匹配的情况.
只需比较DateTime值上的.Date属性:
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();


另外,在c.MealDate上使用.Value似乎暗示MealDate列是可空,并且表达式需要检查它实际上是否具有值.


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.


这篇关于Linq用于选择使用Lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆