【发布时间】:2014-09-10 07:47:21
【问题描述】:
我有这些代码(这只是示例代码)
public SomeService ()
{
public Queryable<CarDto> GetDtos()
{
context.Cars.Select(c => new CarDto
{
CarDtoId = c.Id,
Name = c.Name,
Status = GetCarStatus(c)
})
}
private CarStatusEnum GetCarStatus(Car car)
{
if(car.statusId == 2 || car.statusId == 3)
{
return 4;
}
return 5;
}
}
此代码抛出异常(LINQ to Entities 无法识别 GetCarStatus 方法)。
我知道:
- 为什么会抛出
- 我可以做 ToList() 但我需要 Iqueryble
- 我可以在不使用方法的情况下内联编写方法代码
但我想知道方法我该怎么做,LINQ to Entities 才能识别我的方法?
【问题讨论】:
-
通过实现您自己的查询提供程序
-
你试过简单的三元运算符吗?像这样:
Status = (car.statusId == 2 || car.statusId == 3) ? 4 : 5 -
@IVAAAN123,我指出我可以在不使用方法的情况下内联编写方法代码
-
“我该怎么做,LINQ to Entities 才能识别我的方法?”:你不能...这是纯 C# 代码,无法自动将其转换为 SQL。
标签: c# .net entity-framework linq-to-entities iqueryable