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

次のコードを使用してみてください。これで問題が解決すると確信しています

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 に答える
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();
}

編集: あなたのクエリに基づいて、IN を使用する代わりに JOIN を使用してクエリを表現しても問題ないと考えました

于 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 に答える