Pearl
Pearl

Reputation: 9435

Join and Group By using Lambda Expression

Query

 var grpby4 = from u in dtEmp.AsEnumerable()
                         join v in dtDept.AsEnumerable() on u.Field<int>("DepartmentID") equals v.Field<int>("DepartmentID")
                         group u by v.Field<string>("DeptName") into g
                         select new { DeptName = g.Key, Records = g };

How write the same Query using Lambda Expression?

Upvotes: 2

Views: 3378

Answers (1)

Rawling
Rawling

Reputation: 50114

Using this handy webpage I get

dtEmp.AsEnumerable()
    .Join(dtDept.AsEnumerable(),
        u => u.Field<int>("DepartmentID"),
        v => v.Field<int>("DepartmentID"),
        (u, v) => new { u, v })
    .GroupBy(τ0 => τ0.v.Field<string>("DeptName"), τ0 => τ0.u)
    .Select(g => new { DeptName = g.Key, Records = g })

Upvotes: 2

Related Questions