[英]How do I perform Date Comparison in EF query?
Please help. 请帮忙。 I am trying to figure out how to use DATE or DATETIME for comparison in a linq query. 我试图弄清楚如何在linq查询中使用DATE或DATETIME进行比较。
Example: If I wanted all Employee names for those who started before today, I would do something like this in SQL: 示例:如果我想要在今天之前启动的所有员工姓名,我会在SQL中执行以下操作:
SELECT EmployeeNameColumn
FROM EmployeeTable
WHERE StartDateColumn.Date <= GETDATE() //Today
But what about linq? 但是linq怎么样?
DateTime startDT = //Today
var EmployeeName =
from e in db.employee
where e.StartDateColumn <= startDT
The above WHERE doesn't work: 上面的WHERE不起作用:
Exception Details: System.NotSupportedException: The specified type member 'Date' is not supported in LINQ to Entities. 异常详细信息:System.NotSupportedException:LINQ to Entities中不支持指定的类型成员“Date”。 Only initializers, entity members, and entity navigation properties are supported. 仅支持初始值设定项,实体成员和实体导航属性。
Use the class DbFunctions
for trimming the time portion. 使用DbFunctions
类修剪时间部分。
using System.Data.Entity;
var bla = (from log in context.Contacts
where DbFunctions.TruncateTime(log.ModifiedDate)
== DbFunctions.TruncateTime(today.Date)
select log).FirstOrDefault();
Source: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/84d4e18b-7545-419b-9826-53ff1a0e2a62/ 资料来源: http : //social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/84d4e18b-7545-419b-9826-53ff1a0e2a62/
That should work. 这应该工作。 Are you sure that there isn't another part of the query that triggered the exception? 您确定查询的其他部分没有触发异常吗? I have several instances of queries of the form 我有几个表单查询实例
var query = from e in db.MyTable
where e.AsOfDate <= DateTime.Now.Date
select e;
in my code. 在我的代码中。
It may be due to the date in the database being nullable. 这可能是由于数据库中的日期可以为空。 Try this: 试试这个:
var EmployeeName =
from e in db.employee
where e.StartDateColumn.Value <= startDT
You can check the condition like this 你可以检查这样的情况
var nextDay = DateTime.Today.AddDays(1);
var query = from e in db.MyTable
where e.AsOfDate >= DateTime.Today && e.AsOfDate < nextDay
select e;
here you'll get the records on AsOfDate date as we checking between today(00:00:00) and tommorow(00:00:00) we'll get today's date record only what ever may be the time... 在今天(00:00:00)和tommorow(00:00:00)之间检查时,您将获得AsOfDate日期的记录,我们将获得今天的日期记录...
You can not use .Date 你不能使用.Date
If you would like to check for today you can create a datetime with no time 如果您想今天查看,可以立即创建日期时间
DateTime myDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
var e = (from mds in myEntities.Table
where mds.CreateDateTime >= myDate
select mds).FirstOrDefault();
I'm curious to the error message saying 'Date'
, when you're passing a 'DateTime'
. 当你传递'DateTime'
时,我很想知道'Date'
的错误信息。 Could it be that 'StartDateColumn'
is actually a 'Date'
, rather than a 'DateTime'
in the database? 可能是'StartDateColumn'
实际上是'Date'
,而不是数据库中的'DateTime'
? That might mess up the comparison... 这可能搞砸了比较......
try this: 试试这个:
DateTime dd = DateTime.Parse("08/13/2010 00:00:00");
var data = from n in ContributionEligibilities
where n.ModifiedDateTime.Date >= DateTime.Parse("08/13/2010").Date
select n;
data.Dump("Result") ;
I am using a LinqDataSource and I had problems getting my query with a Date Comparison in it to execute without errors. 我正在使用LinqDataSource,我在使用日期比较查询时遇到问题,无错误地执行。 The answer is to use the WhereAddParameters function and add the test value as a strongly typed parameter. 答案是使用WhereAddParameters函数并将测试值添加为强类型参数。
See the example below where I am matching a groupid and checking to see if the StopDate in my record is greater that or equal to a Date/Time stamp of now. 请参阅下面的示例,其中我匹配groupid并检查我的记录中的StopDate是否大于或等于now的时间戳。
I am using this code fragment currently and it works like a charm. 我目前正在使用这个代码片段,它就像一个魅力。
LinqCampaigns.WhereParameters.Add("StopDate", System.Data.DbType.Date, DateTime.Now.ToString())
LinqCampaigns.Where = "GroupId = " & myGrp & " && " & "StopDate >= @StopDate"
Works like a charm.... 奇迹般有效....
使用局部变量存储Date值,然后在查询中使用该变量:
DateTime today = DateTime.Now.Date; from scheme in context.schemes where scheme.EndDate > today select scheme
ensure that you check null value like this : 确保您检查null值,如下所示:
'(from mm in _db.Calls
where mm.Professionnal.ID.Equals(proid)
&& mm.ComposedDate.HasValue &&
(mm.ComposedDate.Value >= datemin) && (mm.ComposedDate.Value <= date)
select mm).ToArray();'
.Date did not work, but .Day did for me. .Date没有用,但是.Day为我做了。
var query = from o in Payments
where o.Order.OrderDate.Day != o.PaymentDate.Day
orderby o.Order.OrderDate
select new
{
o.Order.OrderID,
o.Order.OrderDate,
o.PaymentDate,
o.Order.FirstName,
o.Order.LastName,
o.Order.CustomerID
};
query.Dump();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.