gpt4 book ai didi

c# - Linq 到实体 SqlFunctions.DatePart

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

此代码产生错误:

void GetLog()
{
List<CAR_STATUS_LOGS> logs = null;
using (TESTEntities ctx = new TESTEntities())
{
logs =
ctx.CAR_STATUS_LOGS
.Where(a => SqlFunctions.DatePart("DAY", a.TIMEMARK) == 1)
.ToList();
}
}

错误是这样的:

LINQ to Entities does not recognize the method 'System.Nullable`1[System.Int32] DatePart(System.String, System.Nullable`1[System.DateTime])' method, and this method cannot be translated into a store expression.

我看不出我对 SqlFunctions.DatePart 做错了什么。 TIMEMARK 列是 Sqlserver DateTime 类型。
有什么想法吗?

最佳答案

您可以使用程序集 EntityFramework.SqlServer.dll( Entity Framework 6.1.3)中的 System.Data.Entity.SqlServer.SqlFunctions,示例:

myTable.Where (w => 
(System.Data.Entity.SqlServer.SqlFunctions.DatePart("weekday", w.Data).Value == 1);

关于c# - Linq 到实体 SqlFunctions.DatePart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24758244/

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