Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# entity framework group by

// This works only in .NET 2.1 or lower
db.Clients.AsNoTracking()
 .Include(x => x.Purchases)
 .Where(x => x.Id == id)
 .GroupBy(x => x.PurchaseId)
 .ToList()
  
// For .NET 3.1 or higher you won`t be able to use GroupBy as in the query above
// Instead you have to convert your query to an enumerable
 db.Clients.AsNoTracking()
 .Include(x => x.Purchases)
 .Where(x => x.Id == id)
 .AsEnumerable() // Retrieves the data and groups them in C#
 .GroupBy(x => x.PurchaseId)
 .ToList()
  
// Or an other technique is to use SelectMany
 db.Clients.AsNoTracking()
 .Include(x => x.Purchases)
 .Where(x => x.Id == id)
 .Select(x => x.Purchases.Id)
 .SelectMany(key => db.Purchases
  .Where(y => y.Id == key))
 .ToList()
Comment

PREVIOUS NEXT
Code Example
Csharp :: get any random item in array c# 
Csharp :: how to convert pdfdocument to binary in c# 
Csharp :: c# bitmap to byte array 
Csharp :: unity key down 
Csharp :: c# console wait for input 
Csharp :: get enum name 
Csharp :: c# array to string 
Csharp :: c# field vs property 
Csharp :: get type of variable c# 
Csharp :: hash table in c# 
Csharp :: c-sharp - get current page url/path/host 
Csharp :: list to list<selectlistitem c# 
Csharp :: create sequence of squares in c# 
Csharp :: console.writeline in c# 
Csharp :: fade image out unity 
Csharp :: flip a character in unity 
Csharp :: c# change label from thread 
Csharp :: check if internet is connected with c# winforms 
Csharp :: c# regex match 
Csharp :: c# cancellationtoken example 
Csharp :: unity color by rgb 
Csharp :: variable gameobject unity 
Csharp :: public gameobject unity 
Csharp :: create list in c# 
Csharp :: c# convert list t to datatable 
Csharp :: declare dictionary c# 
Csharp :: top level statements c# 
Csharp :: convert list string to list long c# 
Csharp :: unity create 3d object in script 
Csharp :: increase value in dictionary against a key in c# 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =