使用Lambda表达式组合没有JOIN的表

时间:2014-11-30 05:43:31

标签: c# linq

组合没有JOIN关键字的表

 var res2 = from u in dtEmp.AsEnumerable()
                       from v in dtDept.AsEnumerable()
                       where u.Field<int>("DepartmentID") == v.Field<int>("DepartmentID") &&
                       u.Field<double>("Salary") > 10000
                       select new
                       {
                           Name = u.Field<string>("Name"),
                           Department = v.Field<string>("DepartmentName")

                       };

如何使用Lambda Expression而不使用Join Keyword?

1 个答案:

答案 0 :(得分:0)

您是否想要从SQL语法切换到Method Chain语法?即:

var res2 = dtEmp.AsEnumerable()
    .SelectMany(u => dtDept.AsEnumerable(), (u, v) => new {u, v})
    .Where(@t => u.Field<int>("DepartmentID") == v.Field<int>("DepartmentID") &&
                u.Field<double>("Salary") > 10000).Select(@t => new
                {
                    Name = u.Field<string>("Name"),
                    Department = v.Field<string>("DepartmentName")
                });