简体   繁体   中英

Converting SQL to Linq-To-SQL

SELECT a.c1, a.c2, b.c1, b.c2
FROM tab1 a, tab2 b
WHERE a.a1 = b.a1 AND a.b1 = b.b2;

I need help converting above to Linq-To-Sql syntax. The problem line is WHERE a.a1 = b.a1 AND a.b1 = b.b2

If it is only one condition, it is easy.

var myQry =
    from a in tab1
    join b in tab2 on a.a1 == b.a1

I need help completing myQry!

You can join on multiple columns like this:

var myQry = 
    from a in tab1 
    join b in tab2 on new { a.a1, a.b1 } equals new { b.a1, b.b2 }
    select new { a.c1, a.c2, b.c1, b.c2 }

Or in fluent syntax:

var myQuery = tab1.Join(tab2, 
                        a => new { a.a1, a.b1 }, 
                        b => new { b.a1, b.b2 },
                        (a, b) => new { a.c1, a.c2, b.c1, b.c2 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM