我有2种DateTime
性质差异
模型下面是代码
sing System.ComponentModel.DataAnnotations.Schema;
namespace GoogleMapsAsp_Net.Models
{
using System;
using System.Collections.Generic;
public partial class Park
{
public int parkId { get; set; }
public decimal Longitude { get; set; }
public decimal Latitude { get; set; }
public Nullable<System.DateTime> TimeStart { get; set; }
public Nullable<System.DateTime> TimeEnd { get; set; }
[NotMapped]
public TimeSpan? Difference { get { return TimeStart - TimeEnd; } }
}
}
我想在几分钟内他们之间的计算差异,谱写新的属性差异。所以我写了这个属性
public TimeSpan? Difference { get { return TimeStart - TimeEnd; } }
但是当我运行的项目我有这个错误
“指定的类型成员‘差异’在LINQ是不支持的实体。只支持初始化器,实体成员和实体导航属性。'
这是我在后端方法,我尝试采取从模型属性差异
public JsonResult GetPlaces()
{
using (var ctx = new GoogleMapsAsp_NetContext())
{
const int coeff = 2;
var items = ctx.Parks.Select(
x => new
{
lng = x.Longitude,
lat = x.Latitude,
difference = x.Difference
}
).ToList();
return Json(items.ToArray(), JsonRequestBehavior.AllowGet);
}
}
我怎样才能正确地计算出它
使用视图模型 - 该属性不应该是您的数据模型的一部分 –
你在'Where'子句中使用'Difference'属性吗?如果是这样,您可以依赖System.Data.Entity.DbFunctions.Diff *函数之一而不是“差异”。 – jbl
目前仍不清楚为何要在数据库级别计算差异。它是否涉及'OrderBy',并带有'Take?涉及coeff? – jbl