c# linq group by from list code example
Example 1: C# linq group by
public class DeliverableGroupContainer
{
public Crew Crew;
public DateTime Date;
public List Deliverables;
}
List Deliveries =
Db.Deliveries
.Where(d => d.CrewId != null)
.GroupBy(d => new {d.Crew, d.Date})
.OrderBy(d => d.Key)
.Select(g => new DeliverableGroupContainer()
{
Crew = g.Key.Crew,
Date = g.Key.Date,
Deliverables = g.ToList()
})
.ToList();
//------------------------------Example----------------------------------
var drAll3 = dt.AsEnumerable().GroupBy(d => new { d.Field("date").Date, V = d.Field("supplierArticle") })
.Select(g => new { _Date = g.Key.Date , Article = g.Key.V, Qty = g.Sum(r => r.Field("quantity")) }).ToList();
DataTable dt4 = StatHellpers.LINQResultToDataTable(drAll3);
Example 2: linq group by
var results = persons.GroupBy(
p => p.PersonId,
p => p.car,
(key, g) => new { PersonId = key, Cars = g.ToList() });