我正在尝试执行以下代码并收到错误
public List<Log> GetLoggingData(DateTime LogDate, string title)
{
var context = new LoggingEntities();
var query = from t in context.Logs
where t.Title == title
&& t.Timestamp == LogDate
select t;
return query.ToList();
}
Run Code Online (Sandbox Code Playgroud)
我收到的错误是"LINQ to Entities不支持指定的类型成员'Date'.仅支持初始值设定项,实体成员和实体导航属性." 我已尝试过将各种符号转换为字符串的各种尝试,只比较日期部分,但似乎无法获得正确的组合.任何帮助是极大的赞赏.
有什么区别:
EntityFunctions.TruncateTime
Run Code Online (Sandbox Code Playgroud)
和
DbFunctions.TruncateTime methods?
Run Code Online (Sandbox Code Playgroud) 我想在我的数据库中选择可用的spotId.我有这个方法:
public ActionResult ShowAvailableSpots(int Id, DateTime ArrivalDate, DateTime LeaveDate)
{
var query2 = (from r in db.Reservations
where (DbFunctions.TruncateTime(r.ArrivalDate) >= DbFunctions.TruncateTime(ArrivalDate)
&& DbFunctions.TruncateTime(r.LeaveDate) <= DbFunctions.TruncateTime(LeaveDate))
select r.spot);
ViewBag.StartingDate = ArrivalDate;
ViewBag.EndingDate = LeaveDate;
ViewBag.AvailableSpots = query2;
ViewBag.CampingSpotId = new SelectList(query2, "CampingSpotId", "SpotName");
return View();
}
Run Code Online (Sandbox Code Playgroud)
我确定在给定的日期范围内没有预订,那么为什么没有返回现场ID?
查询生成的输出如下:
SELECT
[Extent2].[campingspotid] AS [CampingSpotId],
[Extent2].[spotname] AS [SpotName],
[Extent2].[fieldname] AS [FieldName],
[Extent2].[surface] AS [Surface],
[Extent2].[wifi] AS [Wifi],
[Extent2].[water] AS [Water],
[Extent2].[sewer] AS [Sewer],
[Extent2].[reserved] AS [Reserved],
[Extent2].[booked] AS [Booked],
[Extent2].[spotprice] AS [SpotPrice],
[Extent2].[type] AS …Run Code Online (Sandbox Code Playgroud) 我目前正在使用动态 LINQ(原始 codeplex 链接)库来实现动态搜索功能。我有一个要求,我需要OrderBy一个DateTime?只考虑日期而不是时间的字段。我正在使用EntityFramework.
这是示例实体。
public class SampleEntity
{
public DateTime? DateCreated { get; set; }
public bool Flag { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是查询数据库的代码。
var orderByString = "DateCreated.Value.Date asc, Flag"; //This is a dynamic string
var query = _context.Set<SampleEntity>().OrderBy(orderByString);
Run Code Online (Sandbox Code Playgroud)
这个表达式 ("DateCreated.Value.Date") 被解析没有问题,System.Linq.Dynamic但它抛出以下错误(这是可以理解的),因为它不受 LINQ to Entities 支持。请记住,这必须在 IQueryable 上工作(我不能使用这篇文章中的答案),因为我需要在服务器端进行排序。
LINQ to Entities 不支持指定的类型成员“日期”。仅支持初始值设定项、实体成员和实体导航属性。
解决的办法是使用DbFunctions.TruncateTime()如表示在这个答案等。但是,这不会与System.Linq.Dynamic
关于如何解决这个问题的任何想法。可能的解决方案是将另一列添加到只有日期部分的数据库DateCreated并查询该列。但是,我宁愿不这样做,而是在寻找解决此问题的其他方法。另一种方法是动态生成 lambda 表达式并返回 DbFunctions.TruncateTime,然后对 DB …
c# linq linq-to-entities entity-framework entity-framework-6