LINQ 查询在 Dot Net Core 2.2 中运行良好,但在 3.1 中运行良好

LINQ query working fine in Dot Net Core 2.2 but in 3.1

低于 LINQ 查询在 Dot Net Core 2.2 中按预期工作,但当我升级到 3.1 时它抛出运行时执行。

[HttpGet("{SINo}")]
public async Task<IEnumerable<dynamic>> SaleHistory(string SINo)
{
    return await (from s in _context.Sales
                  where s.saleInvNo.Contains("|")
                        ? s.saleInvNo.Split("|", StringSplitOptions.None)[1] == SINo
                        : s.saleInvNo == SINo
                  select new
                  {
                      s.saleDate,
                      s.saleInvNo
                  }).ToListAsync();
}

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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync()

如何更改此翻译table 查询?

注意:销售额 table 有大约 100 万条记录。从中,我只想得到一张唱片。

string.Split 在 LINQ to Entities 中不受支持。所以你需要一起使用 SubstringIndexOf 如下。

使用:

s.saleInvNo.Substring(s.saleInvNo.IndexOf("|") + 1) == SINo

而不是:

s.saleInvNo.Split("|", StringSplitOptions.None)[1] == SINo

参见:

顺便说一句,你应该考虑 @DavidG 建议:

Also looking at this query, I strongly suggest you normalise this data to split out the "SINNo" value into its own column - there's no great way to query this style of data