我在执行 Linq 查询时遇到异常:
The specified type member 'Active' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
模型类
public class User : Entity
{
//Many properties skipped
[NotMapped]
public bool Active
{
get
{
return Orders.Any(c =>
c.Active && (c.TransactionType == TransactionType.Order ||
c.TransactionType == TransactionType.Subscription));
}
}
}
给出异常的 Linq 查询
public IEnumerable<User> GetInactiveUsersForSuspensionNotification()
{
return _userRepository.GetAll()
.Include(i=>i.Orders)
.Where(w => w.Active == false);
}
Orders
是Users
的相关表。
最佳答案
当使用 LINQ to Entities 时,LINQ 表达式被转换为 SQL 查询以发送到数据库。这样整个表就不会被拉入内存。
你的问题是,因为 Active 没有被映射,所以数据库对此一无所知,因此无法进行计算。
您需要将 Active 移动到您的数据库中或将 LINQ 语句更改为仅查询数据库中的列
关于c# - 模型类中的计算字段给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22294740/