al-web
al-web

Reputation: 77

Merge linq results

I got these 2 linq querys. Is there a easy way to merge these 2 querys. So when i'm using my dropdown's i can get a result if both return 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();

}

Upvotes: 0

Views: 82

Answers (2)

Tim Rogers
Tim Rogers

Reputation: 21713

Join to both and use && operator to check both sub-records.

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;

Upvotes: 1

David Colwell
David Colwell

Reputation: 2590

This is a simple example of how to find a document where it is either matched by category or subperson.

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

Upvotes: 0

Related Questions