-3

我有这个 t-sql 查询,我需要将它转换为 linq

SELECT m.field1
            FROM table1 m
            Join table2 v on v.field1 = m.field1 and v.field2 = m.field3
            WHERE v.field4 =4
            AND m.field1=125325
            AND m.field5=0
            AND m.field1 IN (SELECT field1 FROM table3)

有什么帮助吗?

4

2 回答 2

1

询问:

from m in db.table1
join v in db.table2
on new { m.field1, Field2 = m.field2 } 
equals new { v.field1, Field2 = m.field3 }
where ...
where db.table3.Select(x => x.field1).Contains(m.field1)
select m.field1;

方法链:

db.table1
    .Join(
        db.table2, 
        m => new {m.field1, Field2 = m.field2}, 
        v => new {v.field1, Field2 = m.field3},
        (m, v) => new {m, v})
    .Where(...)
    .Where(t => db.table3.Select(x => x.field1).Contains(t.m.field1))
    .Select(t => t.m.field1);
于 2013-06-11T10:34:45.077 回答
1

只要所有表相关,并且您不需要在 LINQ 中做关系,它就会是这样的:

     var result = db.table1.WHERE(p=>p.table2.field4 == 4 && p.field1 == 125325 
     && p.field5 == 0 && p.table3 != null)

哪里db- 是你的DbContextObjectContext

于 2013-06-11T09:41:38.343 回答