Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
(我正在使用 LINQ to SQL 从数据库中检索表)
我需要遍历一个数据库表(有一个字段数据)以将当前日期与表中的日期匹配。我怎样才能做到这一点?
不要使用循环 - 如果表很大,这将非常慢。您想以基于集合的方式执行此操作 - IQueryable .Where() 方法,即:
MyTable.Where(x => x.SomeDate == DateTime.Today);
这假定数据库中的日期仅存储日期部分。如果时间部分存储在数据库中并且您想在比较中忽略它,您需要执行以下操作:
MyTable.Where(x => x.SomeDate >= DateTime.Today && x.SomeDate < DateTime.Today.AddDays(1));