如何将SQL转换为LINQ?

时间:2013-09-06 14:28:50

标签: linq

SQL

select * from (select * from tabla limit 1) as a inner join table b on a.id = b.id where a.id = id

LINQ

from s in tableA
join c in (from a in tableB where a.id==id select a).FirstOrDefault() on s.id equals c.id
where s.id == id
select s

我想将这个sql翻译成linq但是失败了。我怎么做翻译?

1 个答案:

答案 0 :(得分:0)

我认为这样做,但你应该检查linq生成的sql。

a = tableA.FirstOrDefault();

//First option
tableB.Where(xb => xb.id = a.id)
      .Select(xb => new {a = a, b = xb})
      .Where(abx => abx.a.id == id)

//Second option
tableB.Select(xb => new {a = a, b = xb})
      .Where(abx => abx.a.id == id && abx.a.id = abx.b.id)