Greater than in Joins in Lambda Expression

1.3k views Asked by At

Query

 var res4 = from u in dtEmp.AsEnumerable()
                       from v in dtDept.AsEnumerable()
                       let DOJ = u.Field<DateTime>("DOJ")
                       where u.Field<int>("DepartmentID") == v.Field<int>("DepartmentID") &&
                       DOJ > new DateTime(2014, 01, 04)
                       select new
                       {
                           Name = u.Field<string>("Name"),
                           Department = v.Field<string>("DepartmentName")
                       };

How to write the same Query in Lambda Expression?

2

There are 2 answers

0
Rufus L On

Do you mean Method-Chain syntax?

var res4 = dtEmp.AsEnumerable()
    .SelectMany(u => dtDept.AsEnumerable(), (u, v) => new {u, v})
    .Select(@t => new {@t, DOJ = u.Field<DateTime>("DOJ")})
    .Where(@t => u.Field<int>("DepartmentID") == v.Field<int>("DepartmentID") &&
                    DOJ > new DateTime(2014, 01, 04)).Select(@t => new
                    {
                        Name = u.Field<string>("Name"),
                        Department = v.Field<string>("DepartmentName")
                    });
0
Jeffrey Zhang On

You can use Where and Join method:

var res4 = dtEmp.Where(u => u.Field<DateTime>("DOJ") > new DateTime(2014, 01, 04))
   .Join(dtDept, 
         u => u.Field<int>("DepartmentID"), 
         v => v.Field<int>("DepartmentID"), 
        (emp, dept) => new 
        { 
            Name = emp.Field<string>("Name"), 
            Department = v.Field<String>("DepartmentName") 
        });