从.netCore 2.2升级到.NET 5之后,我会收到以下错误:
LINQ表达式'e‘无法翻译。可以用可以翻译的表单重写查询,或者通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用,显式地切换到客户端计算。有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038。
这是我的代码:
var query =
(from cf in ctx.CotacoesFornecedores.IgnoreQueryFilters().AsNoTracking()
join f in ctx.Fornecedores.IgnoreQueryFilters().AsNoTracking() on cf.FornecedorId equals f.Id
join pj in ctx.PessoasJuridicas.IgnoreQueryFilters().AsNoTracking() on f.ParceiroId equals pj.ParceiroId
join c in ctx.Cotacoes.IgnoreQueryFilters().AsNoTracking() on cf.CotacaoId equals c.Id
where filtro.Fornecedores.Contains(cf.FornecedorId)
&& c.EmpresaId == idEmpresa
&& c.Status == Models.StatusCotacao.Liberada.Id
&& codigoStatus.Contains(cf.Status)
&& (!apenasVigentes || (apenasVigentes && c.DataInicial <= dataAtual && c.DataFinal >= dataAtual))
orderby c.Numero, c.DataInicial
select new
{
c.Id,
IdCotacaoFornecedor = cf.Id,
pj.Cnpj,
pj.NomeFantasia,
pj.RazaoSocial,
c.Numero,
c.DataInicial,
c.DataFinal,
cf.Status,
cf.FornecedorId,
cf.PermiteAlterarQuantidadeEmbalagem,
c.Observacoes
});
if (filtro.Id > 0)
query = query.Where(q => q.Id == filtro.Id);
if (filtro.Numero > 0)
query = query.Where(q => q.Numero == filtro.Numero);
if (filtro.DataInicial.GetValueOrDefault() > DateTime.MinValue)
query = query.Where(q => q.DataInicial.Date >= filtro.DataInicial.Value.Date);
if (filtro.DataFinal.GetValueOrDefault() > DateTime.MinValue)
query = query.Where(q => q.DataFinal.Value.Date <= filtro.DataFinal.Value.Date);
return query
.Select(q => new Models.CotacaoFornecedor
{
Id = q.Id,
Numero = q.Numero,
IdFornecedor = q.FornecedorId,
IdCotacaoFornecedor = q.IdCotacaoFornecedor,
CnpjFornecedor = q.Cnpj,
NomeFantasiaFornecedor = q.NomeFantasia,
RazaoSocialFornecedor = q.RazaoSocial,
DataInicial = q.DataInicial,
DataFinal = q.DataFinal,
Status = Models.StatusCotacaoFornecedor.List().SingleOrDefault(e => e.Id == q.Status),
PermiteAlterarQuantidadeEmbalagem = q.PermiteAlterarQuantidadeEmbalagem,
Observacoes = q.Observacoes
}).ToPagedList(filtro);错误指向Status = => e.Id == q.Status),
我知道这是不可能的客户端评估,但我不知道最好的解决方案,使它不打破代码逻辑,有什么建议吗?
发布于 2021-02-10 15:49:14
将客户端逻辑与服务器端分开。很难推断出您的查询需要自动进行后处理。
var pagedList =
.Select(q => new Models.CotacaoFornecedor
{
Id = q.Id,
Numero = q.Numero,
IdFornecedor = q.FornecedorId,
IdCotacaoFornecedor = q.IdCotacaoFornecedor,
CnpjFornecedor = q.Cnpj,
NomeFantasiaFornecedor = q.NomeFantasia,
RazaoSocialFornecedor = q.RazaoSocial,
DataInicial = q.DataInicial,
DataFinal = q.DataFinal,
PermiteAlterarQuantidadeEmbalagem = q.PermiteAlterarQuantidadeEmbalagem,
Observacoes = q.Observacoes
}).ToPagedList(filtro);
foreach (var q in pagedList)
{
q.Status = Models.StatusCotacaoFornecedor.List().SingleOrDefault(e => e.Id == q.Status);
}
return pagedList;发布于 2021-02-11 14:56:53
添加.AsEnumberable解决了我的问题。
https://stackoverflow.com/questions/66139001
复制相似问题