我对linq
实体框架非常陌生。我正在尝试解决以下为什么不起作用的问题。产生的错误是“无法创建'匿名类型'类型的常量值。在此上下文中仅支持原始类型或枚举类型。”
我已经尝试了很多不同的方法,但仍然遇到与原始类型相关的错误。如果有人可以查看下面的代码并希望指出问题所在,我将不胜感激。
public Entities.BikeData[] GetBikesWithExpiredSyncDeadline(int noOfDays) {
using (OfficeEntities cxt = GetContext())
using (ReportingEntities RepCxt = GetReportingContext()) {
Data.Repository.Abstract.IBikeRepository BikeRepos = new Data.Repository.Concrete.BikeRepository();
var details = (from sd in cxt.BikeDetails
where sd.autoreminder == true
&& (sd.lastremindersent == null || sd.lastremindersent < EntityFunctions.AddDays(DateTime.UtcNow, noOfDays * -1))
&& (sd.emailaddress != null && sd.emailaddress.Trim() != "")
select new {
Serial = sd.Serial,
EmailAddress = sd.emailaddress
}).ToList();
var resLst = (from r in RepCxt.RegisteredBikes
join d in details on r.Serial equals d.Serial
join cs in cxt.CompanySettings.ToList() on r.CompanyID equals cs.CompanyID
where (!r.lastupdate.HasValue || r.lastupdate < EntityFunctions.AddDays(DateTime.UtcNow, cs.AutoNotificationFrequency * -1))
select new Entities.BikeData {
ID = r.ID,
Name = r.Ship,
Serial = r.Serial,
LastUpdate = r.lastupdate,
DaysSinceLastSync = (r.lastupdate.HasValue? EntityFunctions.DiffDays(r.lastupdate.Value, DateTime.UtcNow).Value : -1),
EmailAddress = (d.EmailAddress == null ? string.Empty : (String.IsNullOrEmpty(d.EmailAddress) ? r.ShipEmailAddress : d.EmailAddress))
});
return resLst.ToArray();
}
}
更新
我现在通过创建视图采取了不同的方法,因此我不再需要在 EF 中进行跨上下文连接。我希望你能在下面提供帮助。
当我运行 objectQuery.ToTraceString() 时,它为我提供了返回数据库中记录的有效 SQL,但是 EntityFramework 中的 resLst 总是返回 0。关于为什么会发生这种情况有什么明显的吗?
var resLst = (from ls in cxt.BikeLastUpdates
where (!ls.lastupdate.HasValue || ls.lastupdate < EntityFunctions.AddDays(DateTime.UtcNow, ls.AutoNotificationFrequency * -1))
&& (ls.autoreminder ==true)
&& (ls.lastremindersent == null || ls.lastremindersent < EntityFunctions.AddDays(DateTime.UtcNow, 3 * -1))
&& (ls.emailaddress !=null && ls.emailaddress.Trim() != "")
select new Entities.BikeData{
ID = (ls.ID ?? new Guid()),
Name = ls.Bike,
Serial = ls.Serial,
LastUpdate = ls.lastupdate,
EmailAddress = (String.IsNullOrEmpty(ls.emailaddress) ? ls.ShipEmailAddress : ls.emailaddress)
});
var objectQuery = resLst as ObjectQuery;
return resLst.ToArray();