【发布时间】:2016-02-08 12:26:58
【问题描述】:
这真的把我难住了......
我有两个问题..
这个不工作,有错误, “LINQ to Entities 无法识别方法 GetAllCustomers()' 方法,并且该方法无法转换为商店表达式。”
results = theDocuments.Select(x => new DocumentModel()
{
document_id = x.document_id,
document_type = x.document_type,
franchisee_id = x.franchisee_id,
customer_id = x.customer_id,
address = x.address,
area_id = x.area_id,
***HERE***customer_name = outletService.GetAllCustomers().Where(xx => xx.id == x.customer_id).First().name
}).OrderBy(x => x.document_id);
现在像这样重写它:
results = from p in theDocuments
join cust in outletService.GetAllCustomers() on p.customer_id equals cust.id
select new DocumentModel()
{
customer_name = cust.name,
document_id = p.document_id,
document_type = p.document_type,
franchisee_id = p.franchisee_id,
customer_id = p.customer_id,
address = p.address
};
我知道这不能转换为它的相关 SQL,但是,我不确定第二个查询在执行时有什么不同以使其工作,它是否在执行任何其他操作之前执行 GetAllCustomers()?它在内部的工作方式有何不同?
如何重写第一个查询以使其正确执行?
谢谢, 詹姆斯。
**这就是我最终得到的结果**
results = theDocuments.Join(
outletService.GetAllCustomers(), docs => docs.customer_id, customers => customers.id, (doc, cust) => new DocumentModel()
{
document_id = doc.document_id,
document_type = doc.document_type,
franchisee_id = doc.franchisee_id,
customer_id = doc.customer_id,
address = doc.address,
area_id = doc.area_id,
customer_name = cust.name
});
【问题讨论】:
-
join 使用
Join方法,这就是区别 -
如果你声明了一个像
var Name = outletService.GetAllCustomers().Where(xx => xx.id == x.customer_id).First().name这样的变量然后在你的第一个查询中使用它,它会起作用。显然,您需要先获得Name,然后再将其与 lambda 表达式 一起使用
标签: c# linq linq-to-entities iqueryable