pen*_*ate 115 c# linq linq-to-entities entity-framework datetime-comparison
我有两个日期值,一个已存储在数据库中,另一个由用户使用DatePicker选择.用例是从数据库中搜索特定日期.
先前在数据库中输入的值始终具有12:00:00的时间分量,其中从选择器输入的日期具有不同的时间分量.
我只对日期组件感兴趣,并且想要忽略时间组件.
在C#中进行这种比较的方法有哪些?
另外,如何在LINQ中执行此操作?
更新:在LINQ to Entities上,以下工作正常.
e => DateTime.Compare(e.FirstDate.Value, SecondDate) >= 0
Run Code Online (Sandbox Code Playgroud)
Man*_*jua 131
使用该类EntityFunctions修剪时间部分.
using System.Data.Objects;
var bla = (from log in context.Contacts
where EntityFunctions.TruncateTime(log.ModifiedDate) == EntityFunctions.TruncateTime(today.Date)
select log).FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
UPDATE
由于EF 6.0及更高版本EntityFunctions被替换DbFunctions.
Fre*_*örk 121
注意:在撰写此答案时,EF关系不清楚(在编写完成后编辑成问题).要获得EF的正确方法,请检查Mandeeps的答案.
您可以使用该DateTime.Date属性执行仅日期比较.
DateTime a = GetFirstDate();
DateTime b = GetSecondDate();
if (a.Date.Equals(b.Date))
{
// the dates are equal
}
Run Code Online (Sandbox Code Playgroud)
小智 24
我想这可以帮到你.
我做了一个扩展,因为我必须比较填充EF数据的存储库中的日期,所以.Date不是一个选项,因为它没有在LinqToEntities翻译中实现.
这是代码:
/// <summary>
/// Check if two dates are same
/// </summary>
/// <typeparam name="TElement">Type</typeparam>
/// <param name="valueSelector">date field</param>
/// <param name="value">date compared</param>
/// <returns>bool</returns>
public Expression<Func<TElement, bool>> IsSameDate<TElement>(Expression<Func<TElement, DateTime>> valueSelector, DateTime value)
{
ParameterExpression p = valueSelector.Parameters.Single();
var antes = Expression.GreaterThanOrEqual(valueSelector.Body, Expression.Constant(value.Date, typeof(DateTime)));
var despues = Expression.LessThan(valueSelector.Body, Expression.Constant(value.AddDays(1).Date, typeof(DateTime)));
Expression body = Expression.And(antes, despues);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
Run Code Online (Sandbox Code Playgroud)
然后你可以这样使用它.
var today = DateTime.Now;
var todayPosts = from t in turnos.Where(IsSameDate<Turno>(t => t.MyDate, today))
select t);
Run Code Online (Sandbox Code Playgroud)
alg*_*eat 10
如果您使用DateDB Entities 的属性,您将获得异常:
"The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
Run Code Online (Sandbox Code Playgroud)
你可以使用这样的东西:
DateTime date = DateTime.Now.Date;
var result = from client in context.clients
where client.BirthDate >= date
&& client.BirthDate < date.AddDays(1)
select client;
Run Code Online (Sandbox Code Playgroud)
要在LINQ to Entities中执行此操作,您必须使用支持的方法:
var year = someDate.Year;
var month = ...
var q = from r in Context.Records
where Microsoft.VisualBasic.DateAndTime.Year(r.SomeDate) == year
&& // month and day
Run Code Online (Sandbox Code Playgroud)
丑陋,但它的工作原理,它在数据库服务器上完成.
这是一种不同的方法,但只有在SecondDate是您传入的变量时它才有用:
DateTime startDate = SecondDate.Date;
DateTime endDate = startDate.AddDays(1).AddTicks(-1);
...
e => e.FirstDate.Value >= startDate && e.FirstDate.Value <= endDate
Run Code Online (Sandbox Code Playgroud)
我认为这应该有效
小智 5
您可以为此使用 DbFunctions.TruncateTime() 方法。
e => DbFunctions.TruncateTime(e.FirstDate.Value) == DbFunctions.TruncateTime(SecondDate);
Run Code Online (Sandbox Code Playgroud)