[英]how can Calculate difference between two dates (number of days) using linq to entities?
var worker = from w in db.Distributions
join d in db.Distributions on w.WorkerId equals d.WorkerId
join p in db.Products on d.ProductId equals p.ProductId
select new DataBindingProjection
{
Date = d.DateTime,
ProductName = d.Product.ProductName,
DistributedPiece = d.Piece,
HowManyDays = db.Distributions.Where(r => DbFunctions.TruncateTime(r.DateTime) == DateTime.Now.Date)
};
dataGridView1.DataSource = worker.ToList();
//I'm tired for this try various way but still now i face this error..System.NotSupportedException: 'The specified type member 'Date' is not supported in LINQ to Entities. //我厌倦了这种尝试各种方式,但现在我仍然面临这个错误..System.NotSupportedException:'LINQ 不支持实体的指定类型成员'日期'。 Only initializers, entity members, and entity navigation properties are supported.'
仅支持初始化程序、实体成员和实体导航属性。 **Where HowManyDays is string type variable.
**其中 HowManyDays 是字符串类型变量。 How can solve it?
怎么解决? I want to see:Example - "10 days ago" this type answer.
我想看看:例子——“10天前”这个类型的答案。
Try the following solution to get the date difference of two dates:尝试以下解决方案来获取两个日期的日期差异:
var days = from items in Vals
select new
{
days = items.maxdate - items.mindate
};
foreach (var day in days)
{
Console.WriteLine(day.days.TotalDays);
}
According to that error message, linq thinks that Date
property needs to be converted to an SQL statement.根据该错误消息,linq 认为
Date
属性需要转换为 SQL 语句。
Try declaring DateTime.Now.Date
earlier on a separate line before the linq statement, like var nowDate = DateTime.Now.Date
, then reference nowDate
in the linq statement instead of DateTime.Now.Date
尝试在 linq 语句之前的单独行上提前声明
DateTime.Now.Date
,例如var nowDate = DateTime.Now.Date
,然后在 linq 语句中引用nowDate
而不是DateTime.Now.Date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.