我有一组唯一的DateTimes(无时间部分),用户可以从用户界面中选择。这不仅是“LastWeek”或“LastMonth”之类的范围。用户可以选择自己想要的每一天。
在EntityFramework中进行Linq查询以匹配结果的最佳方法是什么?我有一个带有属性CreatedAt的表Foo,该表存储具有时间部分的信息。当然,我不想在客户端检查CreatedAt-Property,SqlServer应该为我完成这项工作。

我认为应该像这样:

var query = _entities.Foo.Where(x => x.UserID == user.ID);

if (selectedDates.IsNullOrEmpty() == false)
    query = query.Where(x => x.CreatedAt 'IsIn' selectedDates);

or:
foreach (var date in selectedDates)
    query = query.Where(x => x.CreatedAt.Year == date.Year && x.Month == date.Month && x.Day == date.Month) //but of course here I have the problem that I have to use the 'Or'-Operator, not 'And'.

我该怎么做?

最佳答案

您可以使用EntityFunctions.TruncateTime()在服务器上执行日期的截断。然后,与此类似的东西应该起作用。

query = query.Where(x => selectedDates.Contains(EntityFunctions.TruncateTime(x));

关于linq - Entity Framework :使用Linq查询一组日期的最佳方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7305120/

10-11 19:40
查看更多