linq中的“where in”子句

时间:2010-08-05 10:02:54

标签: c# linq

下面是我的SQL查询

select * 
from test1View 
where test1ID in (select distinct(test2ID) from test2Result) 

我想在Linq中查询此信息

2 个答案:

答案 0 :(得分:3)

您可以按照以下方式在linq中实现where in功能

   DataClassesDataContext db = new DataClassesDataContext();
        List<int> lstGroups = db.APP3_FRM2GRPs.Where(x => x.FGR_FRM_PKEY == Convert.ToInt32(ddlForms.SelectedValue)).Select(b => b.FGR_GRP_PKEY).ToList<int>();
        var qry = from item in db.APP3_GROUPs
                  where lstGroups.Contains(item.GRP_PKEY)
                  select item;

答案 1 :(得分:0)

IQueryable<int> subQuery = db.Test2Result
  .Select(tr => tr.Test2Id)
  .Distinct();

IQueryable<Test1View> query = db.Test1View
  .Where(tv => subQuery.Contains(tv.Test1ID);