翻译SQL查询到LINQ查询(Translate SQL Query into LINQ Query

2019-09-26 07:53发布

有人可以帮我翻译这个SQL查询到LINQ

select * from server where server.GruppenName like ( select Gruppen.Name from Gruppen where Id = 1 )

我曾尝试以下

db.Server.Where(b => b.GruppenName == db.Gruppen.First(c => c.Id == 1).Name);

但没有运气。

谢谢,

马丁

Answer 1:

db.Server
    .Where
    (
        x=>
            db.Gruppen
            .Where(g=>g.Id == 1)
            .Select(g=>g.Name)
            .Contains(x.GruppenName)
    );


Answer 2:

方法语法

db.Server.Where(s => db.Gruppen.Where(g => g.Id == 1)
                               .Where(g => g.Name.Contains(s.GruppenName))
                               .Any());            

查询语法

var query = from s in db.Server
            where (from g in db.Gruppen
                   where g.Id == 1
                   where g.Name.Contains(s.GruppenName)
                   select s).Any()
            select s;


文章来源: Translate SQL Query into LINQ Query