EF Core 更新无法翻译 LINQ 表达式 'x'
EF Core Update The LINQ expression 'x' could not be translated
我将 .net core 2.2 更新为 5
我有一个关于 ef 的错误
System.InvalidOperationException: 'The LINQ expression 'x' could not
be translated. Either rewrite the query in a form that can be
translated, or switch to client evaluation explicitly by inserting a
call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or
'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for
more information.'
public List<CustomerPageModel> GetCustomers(int AccountID)
{
return (from p in context.Customers
join f in context.Patients on p.ID equals f.CustomerID into ps
from t in ps.DefaultIfEmpty()
where p.AccountID == AccountID
select new CustomerPageModel
{
ID = p.ID,
Name = p.Name,
IsActive = p.IsActive,
TC = p.TC,
Surname = p.Surname,
Email = p.Email,
Address = p.Address,
Phone = p.Phone,
Note = p.Note,
AccountID = p.AccountID,
Pats = string.Join(",", ps.Select(x => x.Name)),
PatCount = ps.Count()
})
.GroupBy(p => p.ID)
.Select(g => g.First())
.ToList();
}
如何转换该代码?
您的问题行是:
Pats = string.Join(",", ps.Select(x => x.Name)),
具体来说,string.Join
方法不会转换为 SQL,因此在以前版本的 EF 中,它必须从数据库中检索数据,然后在内存中预制 string.Join
功能。现在 EF 明确告诉您它不能 运行 在数据库服务器上 - 这是一个重大更改,但一个设计决策告诉您(开发人员)它可能没有 运行 有效如你所想...
要“解决”这个问题,根据您的特定代码示例,我建议如下:
将昵称数组 属性 添加到您的 CustomerPageModel:
public string[] PetNames {get;set;}
并将 Pets
属性 转换为只读计算字符串:
public string Pets { get => string.Join(",", PetNames); }
并将 LINQ 表达式中的问题行更改为:
PetNames = ps.Select(x => x.Name).ToArray()
我改变了(lamb 到子查询)
string.Join(",", ps.Select(x => x.Name))
到
string.Join(",", (from y in PatientList where y.CustomerID == p.ID select y.Name).ToArray()),
我后来建的群(tolist之后)
var test = mylist.GroupBy(p => p.ID)
.Select(g => g.First())
.ToList();
问题已解决
我将 .net core 2.2 更新为 5
我有一个关于 ef 的错误
System.InvalidOperationException: 'The LINQ expression 'x' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
public List<CustomerPageModel> GetCustomers(int AccountID)
{
return (from p in context.Customers
join f in context.Patients on p.ID equals f.CustomerID into ps
from t in ps.DefaultIfEmpty()
where p.AccountID == AccountID
select new CustomerPageModel
{
ID = p.ID,
Name = p.Name,
IsActive = p.IsActive,
TC = p.TC,
Surname = p.Surname,
Email = p.Email,
Address = p.Address,
Phone = p.Phone,
Note = p.Note,
AccountID = p.AccountID,
Pats = string.Join(",", ps.Select(x => x.Name)),
PatCount = ps.Count()
})
.GroupBy(p => p.ID)
.Select(g => g.First())
.ToList();
}
如何转换该代码?
您的问题行是:
Pats = string.Join(",", ps.Select(x => x.Name)),
具体来说,string.Join
方法不会转换为 SQL,因此在以前版本的 EF 中,它必须从数据库中检索数据,然后在内存中预制 string.Join
功能。现在 EF 明确告诉您它不能 运行 在数据库服务器上 - 这是一个重大更改,但一个设计决策告诉您(开发人员)它可能没有 运行 有效如你所想...
要“解决”这个问题,根据您的特定代码示例,我建议如下:
将昵称数组 属性 添加到您的 CustomerPageModel:
public string[] PetNames {get;set;}
并将 Pets
属性 转换为只读计算字符串:
public string Pets { get => string.Join(",", PetNames); }
并将 LINQ 表达式中的问题行更改为:
PetNames = ps.Select(x => x.Name).ToArray()
我改变了(lamb 到子查询)
string.Join(",", ps.Select(x => x.Name))
到
string.Join(",", (from y in PatientList where y.CustomerID == p.ID select y.Name).ToArray()),
我后来建的群(tolist之后)
var test = mylist.GroupBy(p => p.ID)
.Select(g => g.First())
.ToList();
问题已解决