简体   繁体   English

Entity Framework Core 3.1 中的 LINQ group by

[英]LINQ group by in Entity Framework Core 3.1

I have a database table to connect data between user and clients.我有一个数据库表来连接用户和客户端之间的数据。

db: class UserClientCorporate{
 int UserId; 
 User User;
 int ClientCorporateId;
 ClientCorporate ClientCorporate;
}

I want to query to get list of ClientCorporates grouped by userid .我想查询以获取按userid分组的ClientCorporates列表。 I have follow some example on Stack Overflow like Group by in LINQ在 LINQ 中遵循了诸如Group by 之类的 Stack Overflow 上的一些示例

and here is my query:这是我的查询:

var data3 = from db in _context.UserClientCorporate
            group db.ClientCorporateId by db.UserId into g
            select new { UserId = g.Key, Clients = g.ToList() };

return Ok(await data3.ToListAsync());

When I run this, I got error:当我运行这个时,我得到了错误:

fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLT67LJQA4IP", Request id "0HLT67LJQA4IP:0000000F": An unhandled exception was thrown by the application.失败:Microsoft.AspNetCore.Server.Kestrel[13] 连接 ID“0HLT67LJQA4IP”,请求 ID“0HLT67LJQA4IP:0000000F”:应用程序引发了未处理的异常。 System.InvalidOperationException: The LINQ expression 'ToList(GroupByShaperExpression: KeySelector: u.UserId, ElementSelector:ProjectionBindingExpression: EmptyProjectionMember )' could not be translated. System.InvalidOperationException: 无法翻译 LINQ 表达式“ToList(GroupByShaperExpression: KeySelector: u.UserId, ElementSelector:ProjectionBindingExpression: EmptyProjectionMember)”。 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().以可翻译的形式重写查询,或通过插入对 AsEnumerable()、AsAsyncEnumerable()、ToList() 或 ToListAsync() 的调用显式切换到客户端评估。 See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.有关详细信息,请参阅https://go.microsoft.com/fwlink/?linkid=2101038

How to solve this problem?如何解决这个问题呢?

SOLVED !解决了 ! After I did more research it seems EF Core has limitation doing this query on database server.在我做了更多研究之后,似乎 EF Core 在数据库服务器上执行此查询有限制。 so I need to get the data first and processed it on my dotnet server (client).所以我需要先获取数据并在我的 dotnet 服务器(客户端)上处理它。

Here is the这里是

var data = await _context.UserClientCorporate.Include(x => x.User).Include( x => x.ClientCorporate).
var res2 = from db in data 
            group db by db.UserId into g
            select new {UserId = g.Key, Clients = g};

Client side GroupBy is not supported in .netcore 3.1 .netcore 3.1 不支持客户端 GroupBy

You may write your query as simple as this:您可以像这样简单地编写查询:

var data3 = __context.UserClientCorporate.ToList().GroupBy(x => x.UserId);

Code writter in C# is client side. C# 中的代码编写者是客户端。

Delete this .ToList() :删除这个.ToList()

var data3 = from db in _context.UserClientCorporate
            group db.ClientCorporateId by db.UserId into g
            select new { UserId = g.Key, Clients = g };

return Ok(await data3.ToListAsync());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM