我正在 LINQPad 中编写这个 LINQ 查询。我的操作类似于:
var perms = (from x in db.TableName // etc
select new ProfilePermission
{ // etc
});
perms = perms.Concat(from x in db.TableName // etc
select new ProfilePermission
{ // etc
});
var results = (from pi in db.AnotherTable
where pi.IsActive
select new MyViewModel
{
KeyId = pi.Id,
Permissions = (from pm in perms
where pi.ChildId == pm.ChildId
select pm)
}
使用这个子查询是可行的。因此,我想,将它移动到扩展方法中去。我尝试了以下代码:
public static IQueryable<ProfilePermission> GetProfilePermissions
(
this IMkpContext db
)
{
var perms = (from x in db.TableName // etc
select new ProfilePermission
{ // etc
});
perms = perms.Concat(from x in db.TableName // etc
select new ProfilePermission
{ // etc
});
return perms;
}
var results = (from pi in db.AnotherTable
where pi.IsActive
select new MyViewModel
{
KeyId = pi.Id,
Permissions = (from pm in db.GetProfilePermissions()
where pi.ChildId == pm.ChildId
select pm)
}
现在我收到一条消息:
NotSupportedException: LINQ to Entities不认识方法'System.Linq.IQueryable`1[PublicationSystem.Model.ViewModels.ProfilePermission] GetProfilePermissions(PublicationSystem.Model.IMkpContext)', 这个方法无法转换为存储表达式。
为什么子查询可以一种方式工作,而另一种方式却不能?我认为perms最终都会成为
IQueryable<>
。