我有一组唯一的日期时间(没有时间部分),用户可以从用户界面中选择。这不仅仅是像“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 : Best way to query for a set of dates using Linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305120/