LINQ查询表达式为显式点符号

时间:2012-08-20 11:35:43

标签: c# .net linq

你能告诉我如何将下面的查询转换为Linq方法版本:

 myResult =
    from table1 in db.TABLE1
    join table2 in db.TABLE2 on table1.TABLE2_ID equals table2.ID
    join table3 in db.TABLE3 on table1.TABLE3_ID equals table3.ID
    where table1.ACTIF == true
    select new MyClass
    {
         T1MyField1 = table1.MyField1,
         T1MyField2 = table1.MyField2,
         T2MyField1 = table2.MyField1,
         T2MyField2 = table2.MyField2,
         T3MyField1 = table3.MyField1,
         T3MyField2 = table3.MyField2,
         T3MyField3 = table3.MyField3
    }

更新1: 我创建了一个谓词:

 Expression<Func<IMyClass, bool>> predicate;

我想将它应用于lambada,但我认为使用lambda语法是不可能的。

3 个答案:

答案 0 :(得分:1)

您可以使用ILSpy使用查询语法将当前程序集反编译为方法语法查询。只需转到选项,然后取消选择“反编译查询表达式”。

答案 1 :(得分:1)

var myResult = db.TABLE1
    .Join(db.TABLE2, t1 => t1.TABLE2_ID, t2 => t2.ID, (t1, t2) => new { Table1 = t1, Table2 = t2 })
    .Join(db.TABLE3, j => j.Table1.TABLE3_ID, t3 => t3.ID, (j, t3) => new { Table1 = j.Table1, Table3 = j.Table2, Table3 = t3 })
    .Where(row => row.Table1.ACTIF)
    .Select(row => new MyClass {
        T1MyField1 = row.Table1.MyField1,
        T1MyField2 = row.Table1.MyField2,
        T2MyField1 = row.Table2.MyField1,
        ...

    });

答案 2 :(得分:0)

关于你的编辑:你可以随意混合两种风格:

myResult = (from ... select new MyClass { ... }).Where(predicate);