Linq根据另一个表中的值选择数据

时间:2014-12-22 15:22:55

标签: c# linq

我有下面列出的linq语句,我想根据另一个表中的值限制一个表中的记录。这是我的代码:

using (context = new DocEntityConnection())
{
   var Docs = context.tbDocsDetails.Where(md => md.IsCurrentDetails && md.tbDoc.StatusID == 9).ToList(); 
   this.approves = context.tbDocApproves.ToList().Where(a => Docs.Select(x => x.DocID).ToList().Contains(a.DocID)).ToList();
   return Docs.Select(md => GetDataItem(md)).ToList();
}

另一个名为tbDocStatus的表格也有DocId字段 我只想从tbDocDetails tbDocdetails.DocId = tbDocStatus.DocIdtbDocStatus.StatusId = 4返回记录。

我如何将其添加到上面显示的代码中?

2 个答案:

答案 0 :(得分:0)

您需要的是join

var Docs = from docDetail in context.tbDocsDetails
           join docStatus in context.tbDocStatus
           on docDetail.DocId = docStatus.DocId
           where docStatus.StatusId == 4 
           select docDetail;

答案 1 :(得分:0)

using (context = new DocEntityConnection())
{
var Docs = context.tbDocsDetails.Where(md => md.IsCurrentDetails && md.tbDoc.StatusID ==  9).ToList(); 

var DocsFiltered = from d in Docs
               join docStatus in context.tbDocStatus
               on d.DocId equals docStatus.DocId
               where docStatus.StatusId = 4
               select d

this.approves = context.tbDocApproves.ToList().Where(a => Docs.Select(x =>       x.DocID).ToList().Contains(a.DocID)).ToList();
return DocsFiltered.Select(md => GetDataItem(md)).ToList();
}