gpt4 book ai didi

entity-framework - GroupBy 表达式无法翻译

转载 作者:行者123 更新时间:2023-12-03 23:49:36 26 4
gpt4 key购买 nike

//Model
public class Application
{
[Key]
public int ApplicationId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ConfirmedDate { get; set; }
public DateTime IssuedDate { get; set; }
public int? AddedByUserId { get; set; }
public virtual User AddedByUser { get; set; }
public int? UpdatedByUserId { get; set; }
public virtual User UpdatedByuser { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string TRN { get; set; }
public string EmailAddress { get; set; }
public string Address { get; set; }
public int ParishId { get; set; }
public Parish Parish { get; set; }
public int? BranchIssuedId { get; set; }
public BranchLocation BranchIssued { get; set; }
public int? BranchReceivedId { get; set; }
public BranchLocation BranchReceived {get; set; }
}

public async Task<List<Application>> GetApplicationsByNameAsync(string name)
{
if (string.IsNullOrEmpty(name))
return null;
return await _context.Application
.AsNoTracking()
.Include(app => app.BranchIssued)
.Include(app => app.BranchReceived)
.Include(app => app.Parish)
.Where(app => app.LastName.ToLower().Contains(name.ToLower()) || app.FirstName.ToLower()
.Contains(name.ToLower()))
.GroupBy(app => new { app.TRN, app })
.Select(x => x.Key.app)
.ToListAsync()
.ConfigureAwait(false);
}

上述 GroupBy 表达式无法在 VS Studio 中编译。我的目标是按包含用户给定字符串的名称运行查询过滤结果,然后它应该通过类似的 TRN 数字对结果进行分组,返回这些应用程序的列表以返回到 View 。我想我真的很接近,但似乎无法弄清楚查询的最后一点。任何指导表示赞赏。

出现错误
InvalidOperationException: The LINQ expression 'DbSet<Application>
.Where(a => a.LastName.ToLower().Contains(__ToLower_0) || a.FirstName.ToLower().Contains(__ToLower_0))
.GroupBy(
source: a => new {
TRN = a.TRN,
app = a
},
keySelector: a => a)' 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()

更新
似乎这绝对是由于自最近更新以来 .net core 3.x 和 EF core 一起玩的方式发生了变化。我不得不使用 AsEnumerable() 而不是 ToListAsync() 将其更改为客户端评估。 Steve py 给出的查询的其余部分使用此方法。即使在阅读文档后,我也不知道 groupby 是如何在 LINQ 中真正工作的,所以这对我有很大帮助。然而,将查询带到客户端 eval 可能会出现性能问题。

最佳答案

基于此:

I want to group by TRN which is a repeating set of numbers eg.12345, in the Application table there may be many records with that same sequence and I only want the very latest row within each set of TRN sequences.



我相信这应该可以满足您的需求:
return await _context.Application
.AsNoTracking()
.Include(app => app.BranchIssued)
.Include(app => app.BranchReceived)
.Include(app => app.Parish)
.Where(app => app.LastName.ToLower().Contains(name.ToLower()) || app.FirstName.ToLower()
.Contains(name.ToLower()))
.GroupBy(app => app.TRN)
.Select(x => x.OrderByDescending(y => y.CreatedAt).First())
.ToListAsync()
.ConfigureAwait(false);
GroupBy 表达式应该代表你想要的分组依据。在你的情况下,TRN。从那里开始,当我们进行选择时, x 代表每个“组”,其中包含属于每个 TRN 的可枚举应用程序集。因此,我们按 CreatedAt 日期的降序对它们进行排序,以使用 First 选择最新的。

试一试。如果这不是您想要的,请考虑为您的问题添加一个示例集,以及所需的输出与此处产生的输出/错误。

关于entity-framework - GroupBy 表达式无法翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59633712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com