Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have follow some example on Stack Overflow like Group by in LINQ

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. System.InvalidOperationException: The LINQ expression 'ToList(GroupByShaperExpression: KeySelector: u.UserId, ElementSelector:ProjectionBindingExpression: EmptyProjectionMember )' 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(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

How to solve this problem?

SOLVED ! After I did more research it seems EF Core has limitation doing this query on database server. so I need to get the data first and processed it on my dotnet server (client).

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};
like image 676
nightingale2k1 Avatar asked Jan 31 '20 11:01

nightingale2k1


People also ask

Can we use LINQ with Entity Framework?

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context.

What does LINQ GroupBy do?

Grouping is a very powerful feature provided by LINQ that transforms a collection in groups where each group has a key associated with it. Note: GroupBy performs a deferred execution which means the query is only executed when the records are processed using a foreach loop.

Can we use LINQ in .NET core?

Using LINQ in MVC . NET Core LINQ stands for Language Integrated Query. Language Integrated Query is one structured query that is used to retrieve data from the database and other different sources and formats. LINQ tutorials will assist you to find out the LINQ language using topics that go from basic to advanced.

What is LINQ Group join?

The group join is useful for producing hierarchical data structures. It pairs each element from the first collection with a set of correlated elements from the second collection. For example, a class or a relational database table named Student might contain two fields: Id and Name .


1 Answers

Client side GroupBy is not supported in .netcore 3.1

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.

like image 200
Shahid Malik Avatar answered Oct 23 '22 15:10

Shahid Malik