1

我有以下 SQL 语句

    SELECT [CodeHouse]
          ,[CodeReq]
          ,[Address]
      FROM [ShahrdariProject].[dbo].[House]
      where [CodeReq] in
      (select [CodeReq] From [ShahrdariProject].[dbo].[HouseOwner] where [Name] = 'Alex' )

谁能帮我将此语句转换为 LINQ?

4

4 回答 4

2

这个 SQL:

 SELECT [CodeHouse]
      ,[CodeReq]
      ,[Address]
  FROM [ShahrdariProject].[dbo].[House]
  where [CodeReq] in
  (select [CodeReq] From [ShahrdariProject].[dbo].[HouseOwner] 
   where [Name] = 'Alex' )

相当于这个 Linq:

var q = from h in db.Houses
        where db.HouseOwners.Any(x => x.Name == "Alex" && x.CodeReq == h.CodeReq)
        select h;
于 2012-07-09T04:57:28.507 回答
1
using(var dbContext = new /**Your Linq DataContext class here**/())
{

    var results = dbContext.House.Join(
                dbContext.HouseOwner.Where(ho => ho.Name == "Alex"), 
                h => h.CodeReq, 
                ho => ho.CodeReq,
                (h, ho) => select new { h.CodeHouse, h.CodeReq, h.Address }).ToArray();
}

编辑:根据您的查询,我认为可以使用 JOIN 而不是使用 IN 来表达查询

于 2012-07-09T04:54:17.870 回答
1
List<string> codeReq = new List<string>();
using(DemoDataContext db = new DemoDataContext()){
var houses = from h db.Houses where codeReq.Contains(h.codeReq) selec h;
}
于 2012-07-09T04:57:35.857 回答
1

尝试使用以下代码,我相信这可以解决您的问题

var result = from house in db.Houses
    where db.HouseOwners.Any(z => z.Name == "Alex" && z.CodeReq == house.CodeReq)
    select house;

享受......

于 2012-07-09T05:50:25.280 回答