gpt4 book ai didi

c# - Entity Framework 日期解析

转载 作者:太空狗 更新时间:2023-10-30 00:35:27 25 4
gpt4 key购买 nike

我正在尝试执行 LINQ to Entities 查询(我对 EF 还是很陌生,并且我正在学习它与 LinqToSQL 的不同之处)并且我正在尝试做这样的事情:

DateTime begin = new DateTime(2011, 1, 1);

Apps = (from app in context.Instances
where app.ReleaseDate.Date == begin
select new ScalpApp
{
Image = app.Image,
PublisherName = app.PublisherName,
}).ToList();

虽然这在 LinqPad 中有效,但在我的代码中无效。抛出的异常是:

The specified type member 'Date' is not supported in LINQ to Entities.

Only initializers, entity members, and entity navigation properties are supported.

我如何在 LinqToEF 中执行此操作?数据库中的字段是完整的日期时间(带有时间码),我试图仅根据日期进行解析。

最佳答案

您不能简单地使用“app.ReleaseDate.Data”,但您可以:

var begin = new DateTime(2011, 1, 1);
var end = begin.AddHours(24);

Apps = (from app in context.Instances
where
(app.ReleaseDate >= begin) and
(app.ReleaseDate < end)
select new ScalpApp
{
Image = app.Image,
PublisherName = app.PublisherName,
}).ToList();

关于c# - Entity Framework 日期解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4644211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com