Linq 查询重写
Linq query rewriting
这真的难倒我了...
我有两个问题..
这个不工作,有错误,
"LINQ to Entities does not recognize the method GetAllCustomers()' method, and this method cannot be translated into a store expression."
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
});
第一个尝试将您的方法转换为 SQL 方法,第二个尝试在查询中创建连接。在我看来,正确的方法是使用 join :)
我是个问题。我找到了解决办法。这可能对你有帮助。
dynamic user = new
{
Name = "",
Surname = ""
};
dynamic userModel = new
{
Name = "",
Surname = "",
FullName = ""
};
var query = db.User.AsNoTracking();
query = query.Select(x => new userModel
{
Name = x.Name,
Surname = x.SurName,
FullName = $"{x.Name}{x.SurName}",
}).ToList();
如果您尝试执行此查询。你会崩溃的。 Linq 不支持
FullName = $"{x.Name}{x.SurName}"
您必须将此行更改为
FullName = x.Name + " " + x.SurName
这真的难倒我了...
我有两个问题..
这个不工作,有错误, "LINQ to Entities does not recognize the method GetAllCustomers()' method, and this method cannot be translated into a store expression."
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
});
第一个尝试将您的方法转换为 SQL 方法,第二个尝试在查询中创建连接。在我看来,正确的方法是使用 join :)
我是个问题。我找到了解决办法。这可能对你有帮助。
dynamic user = new
{
Name = "",
Surname = ""
};
dynamic userModel = new
{
Name = "",
Surname = "",
FullName = ""
};
var query = db.User.AsNoTracking();
query = query.Select(x => new userModel
{
Name = x.Name,
Surname = x.SurName,
FullName = $"{x.Name}{x.SurName}",
}).ToList();
如果您尝试执行此查询。你会崩溃的。 Linq 不支持
FullName = $"{x.Name}{x.SurName}"
您必须将此行更改为
FullName = x.Name + " " + x.SurName