-3

I have this t-sql query and I need to convert it to 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)

any help ?

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
Amr Elnashar
  • 1,739
  • 12
  • 33
  • 53

2 Answers2

1

As long as all tables related, and you don't need to do relations it LINQ, so it will be something like:

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

Where db - is your DbContext or ObjectContext

Uriil
  • 11,948
  • 11
  • 47
  • 68
1

Query:

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;

Method chain:

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);
lisp
  • 4,138
  • 2
  • 26
  • 43