Answers for "groupby in linq"

4

groupby in linq

//Method Syntax

        List<Result> results2 = persons
            .GroupBy(p => p.PersonId, 
                     (k, c) => new Result()
                             {
                                 PersonId = k,
                                 Cars = c.Select(cs => cs.car).ToList()
                             }
                    ).ToList();
Posted by: Guest on July-13-2020
2

groupby in linq

var results = from p in persons
              group p.car by p.PersonId into g
              select new { PersonId = g.Key, Cars = g.ToList() };
Posted by: Guest on July-13-2020
2

C# linq group by

public class DeliverableGroupContainer
{
    public Crew Crew;
    public DateTime Date;
    public List<Deliverable> Deliverables;
}

List<DeliverableGroupContainer> 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<DateTime>("date").Date, V = d.Field<String>("supplierArticle") })
                         .Select(g => new { _Date = g.Key.Date , Article = g.Key.V, Qty = g.Sum(r => r.Field<int>("quantity")) }).ToList();


            DataTable dt4 = StatHellpers.LINQResultToDataTable(drAll3);
Posted by: Guest on July-28-2020
1

linq group by

var results = persons.GroupBy(
    p => p.PersonId, 
    p => p.car,
    (key, g) => new { PersonId = key, Cars = g.ToList() });
Posted by: Guest on September-03-2020

Browse Popular Code Answers by Language