Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I just LINQ Join() to link two IQueryables?

I have two IQueryables:

Ingredient:

IngId
Description

AvailableIngredient:

IngId

I already have an IQueryable for Ingredient:

var ingQuery = from i in context.Ingredients
               select i;

How can I add a join to his so it filters by AvailableIngredient (i.e. an Inner Join)? I know how to do it if I had to join all the time, i.e. from... join context.Available... etc), but the Join is conditional, so I need to use the other syntax:

if (filterByAvailable)
{
   IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
   ingQuery = ingQuery.Join(...); // Can I use this to join to the query?
}

This may not be the right method, so this is what I want to do:

  • GetAvailableIngredientQuery returns the available ingredients query, i.e. 3000 of 6000 (but it doesn't enumerate the results yet as it's returned as an IQueryable from EF)
  • Join the availableQuery to the ingQuery, so there's an Inner Join between the two queries

EDIT:

This is the code I'm currently using (very fast), but it means duplicated code:

IQueryable<Ingredient> query;
if (filterByAvailable)
{
    IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
    query = from item in context.Ingredients
               // Quite a few `where` clauses and stuff
            join t in availableQuery on item.IngId equals t.IngId
            select item;
}
else
{ 
    query = from item in context.Ingredients
               // The SAME `where` clauses and stuff as above
            select item;
}
like image 658
djdd87 Avatar asked Oct 08 '10 10:10

djdd87


People also ask

How do you do Joins in LINQ?

In a LINQ query expression, join operations are performed on object collections. Object collections cannot be "joined" in exactly the same way as two relational tables. In LINQ, explicit join clauses are only required when two source sequences are not tied by any relationship.

How do I combine two Iqueryables?

How to merge two IQueryable lists in LINQ? By using Concat() Or Union()var filter1 = from p in db. table1 var filter2 = from p in db. table2var filter3 = filter1.

How Use left and right join in LINQ?

In LINQ, LEFT JOIN or LEFT OUTER JOIN is used to return all the records or elements from the left side collection and matching the elements from the right side of the collection. In LINQ, to achieve the LEFT Join behavior, it is mandatory to use the "INTO" keyword and "DefaultfEmpty()" method.


1 Answers

Use the first query as the source of the subsequent query.

IQueryable<Ingredient> query = from item in context.Ingredients
                             // Quite a few `where` clauses and stuff
                               select item;

if (filterByAvailable)
{
    IQueryable<Available> availableQuery = GetAvailableIngredientQuery(context);
    query = from item in query
            join t in availableQuery on item.IngId equals t.IngId
            select item;
}
like image 118
Clicktricity Avatar answered Sep 28 '22 06:09

Clicktricity