合并linq结果

时间:2014-02-24 11:43:20

标签: c# asp.net linq

我收到了这2个linq查询。有没有一种简单的方法来合并这两个查询。因此,当我使用我的下拉列表时,如果两者都返回true,我可以得到结果吗?

public void CategorySort()
{
    var categoryId = int.Parse(ddlCat.SelectedValue);

    var data = new MyModelContext();

    var cat = from c in data.tblDocuments
              join sc in data.tblSubCategories on c.DocId equals sc.DocId
              where sc.CategoryId == categoryId
              select c;

    rptResult.DataSource = cat.ToList();
    rptResult.DataBind();
}

 public void SortPerson()
{
    var personId = int.Parse(ddlPerson.SelectedValue);
    var data = new MyModelContext();


    var documents = from d in data.tblDocuments
                    join sp in data.tblSubPersons on d.DocId equals sp.DocId
                    where sp.PersonId == personId
                    select d;

    rptResult.DataSource = documents.ToList();
    rptResult.DataBind();

}

2 个答案:

答案 0 :(得分:1)

加入两者并使用&&运算符检查两个子记录。

var cat = from c in data.tblDocuments
          join sc in data.tblSubCategories on c.DocId equals sc.DocId
          join sp in data.tblSubPersons on d.DocId equals sp.DocId
          where sc.CategoryId == categoryId && sp.PersonId == personId
          select c;

答案 1 :(得分:0)

这是一个简单的示例,说明如何找到按类别或子员名匹配的文档。

var cat = from c in data.tblDocuments
          join sc in data.tblSubCategories on c.DocId equals sc.DocId
          join sp in data.tblSubPersons on c.DocId equals sp.DocId
          where sc.CategoryId == categoryId || sp.PersonId == personId
          select c; //This needs to change to be what you want to select