C#在LINQ中调用方法
c# calling method within LINQ
我正在使用 LINQ,我想通过传递参数来调用一个方法(例如 checkOpenClose
),并让它 return true
/false
:
var model = (from account in _ctx.Account
where (...)
select new DetailVM
{
Id = account.Id,
Name = account.CompanyName,
OpenNow = checkOpenClose(account.MonOpen, account.MonClosed,
account.TueOpen, account.TueClosed,
account.WedOpen, account.WedClosed,
account.ThuOpen, account.ThuClosed,
account.FriOpen, account.FriClosed,
account.SatOpen, account.SatClosed,
account.SunOpen, account.SunClosed)
}).Tolist()
我收到以下错误:
LINQ to Entities does not recognize the method 'Boolean checkOpenClose
(System.String, System.String, System.String, System.String,
System.String, System.String, System.String, System.String,
System.String, System.String, System.String, System.String,
System.String, System.String)' method, and this method cannot be
translated into a store expression.
我做错了什么?
请记住,查询将被转换为 sql,因此您的 Linq 提供程序无法将您的方法调用转换为有效的内容。
如果您真的需要调用该方法,那么我建议调用 AsEnumerable
扩展方法,如下所示:
var model= (from account in _ctx.Account
where (...)
select account)
.AsEnumerable()
.Select(account=> new DetailVM
{
Id = account.Id,
Name = account.CompanyName,
OpenNow = checkOpenClose(account.MonOpen, account.MonClosed,
account.TueOpen, account.TueClosed,
account.WedOpen, account.WedClosed,
account.ThuOpen, account.ThuClosed,
account.FriOpen, account.FriClosed,
account.SatOpen, account.SatClosed,
account.SunOpen, account.SunClosed)
})
.Tolist();
这将允许您使用 Linq to Objects 执行您需要的投影
您的布尔方法中的表达式没有 SQL 对应项...这已在 SO 上充实了几次,请参阅
EF needs expressions that have an SQL equivalent
或
another from SO
正如我所见,您正在使用 entity framework datacontext,它将 linq 语句转换为 sql 脚本,因此当您使用方法时,它无法将其转换为 sql。
试试这个
var model= (from account in _ctx.Account
where (...)
select new {
Id = account.Id,
Name = account.CompanyName,
MO = account.MonOpen,
MC = account.MonClosed,
TUO = account.TueOpen,
TUC = account.TueClosed,
WO = account.WedOpen,
WC = account.WedClosed,
THO = account.ThuOpen,
THC = account.ThuClosed,
FO = account.FriOpen,
FC = account.FriClosed,
SO = account.SatOpen,
SC = account.SatClosed,
SUO = account.SunOpen,
SUC = account.SunClosed
}).Tolist().select(m=> new DetailVM {
Id = account.Id,
Name = account.CompanyName,
OpenNow = checkOpenClose(MO,MC,TUO,TUC,WO,WC,THO,THC,FO,FC,SO,SC,SUO,SUC)
}).ToList();
我正在使用 LINQ,我想通过传递参数来调用一个方法(例如 checkOpenClose
),并让它 return true
/false
:
var model = (from account in _ctx.Account
where (...)
select new DetailVM
{
Id = account.Id,
Name = account.CompanyName,
OpenNow = checkOpenClose(account.MonOpen, account.MonClosed,
account.TueOpen, account.TueClosed,
account.WedOpen, account.WedClosed,
account.ThuOpen, account.ThuClosed,
account.FriOpen, account.FriClosed,
account.SatOpen, account.SatClosed,
account.SunOpen, account.SunClosed)
}).Tolist()
我收到以下错误:
LINQ to Entities does not recognize the method 'Boolean checkOpenClose (System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String, System.String)' method, and this method cannot be translated into a store expression.
我做错了什么?
请记住,查询将被转换为 sql,因此您的 Linq 提供程序无法将您的方法调用转换为有效的内容。
如果您真的需要调用该方法,那么我建议调用 AsEnumerable
扩展方法,如下所示:
var model= (from account in _ctx.Account
where (...)
select account)
.AsEnumerable()
.Select(account=> new DetailVM
{
Id = account.Id,
Name = account.CompanyName,
OpenNow = checkOpenClose(account.MonOpen, account.MonClosed,
account.TueOpen, account.TueClosed,
account.WedOpen, account.WedClosed,
account.ThuOpen, account.ThuClosed,
account.FriOpen, account.FriClosed,
account.SatOpen, account.SatClosed,
account.SunOpen, account.SunClosed)
})
.Tolist();
这将允许您使用 Linq to Objects 执行您需要的投影
您的布尔方法中的表达式没有 SQL 对应项...这已在 SO 上充实了几次,请参阅
EF needs expressions that have an SQL equivalent
或
another from SO
正如我所见,您正在使用 entity framework datacontext,它将 linq 语句转换为 sql 脚本,因此当您使用方法时,它无法将其转换为 sql。 试试这个
var model= (from account in _ctx.Account
where (...)
select new {
Id = account.Id,
Name = account.CompanyName,
MO = account.MonOpen,
MC = account.MonClosed,
TUO = account.TueOpen,
TUC = account.TueClosed,
WO = account.WedOpen,
WC = account.WedClosed,
THO = account.ThuOpen,
THC = account.ThuClosed,
FO = account.FriOpen,
FC = account.FriClosed,
SO = account.SatOpen,
SC = account.SatClosed,
SUO = account.SunOpen,
SUC = account.SunClosed
}).Tolist().select(m=> new DetailVM {
Id = account.Id,
Name = account.CompanyName,
OpenNow = checkOpenClose(MO,MC,TUO,TUC,WO,WC,THO,THC,FO,FC,SO,SC,SUO,SUC)
}).ToList();