【发布时间】:2010-08-01 20:07:49
【问题描述】:
给定以下查询:
var query = from item in context.Users // Users if of type TblUser
select new User() // User is the domain class model
{
ID = item.Username,
Username = item.Username
};
如何在其他查询中重复使用语句的 select 部分?即
var query = from item in context.Jobs // Jobs if of type TblJob
select new Job() // Job is the domain class model
{
ID = item.JobId,
User = ReuseAboveSelectStatement(item.User);
};
我尝试使用映射器方法:
public User MapUser(TblUser item)
{
return item == null ? null : new User()
{
ID = item.UserId,
Username = item.Username
};
}
与:
var query = from item in context.Users // Users if of type TblUser
select MapUser(item);
但是如果我这样做,那么框架会抛出一个错误,例如:
LINQ to Entities 无法识别 方法'MapUser(TblUser)'方法, 而且这个方法不能翻译 到商店表达式中。
【问题讨论】:
标签: linq entity-framework linq-to-entities c#-4.0 entity-framework-4