Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq join table with collection

Tags:

linq

I have a collection that I'm gathering from a linq query and I'd like to use that collection as a join for another query. How do I do that?

Thanks

like image 965
user471807 Avatar asked Nov 17 '10 00:11

user471807


2 Answers

LINQ 101 Samples

Copied LINQ statements from here

string[] categories = new string[]{ 
    "Beverages", 
    "Condiments", 
    "Vegetables", 
    "Dairy Products", 
    "Seafood" };

List<Product> products = GetProductList();

var q =
    from c in categories
    join p in products on c equals p.Category
    select new { Category = c, p.ProductName };

foreach (var v in q)
{
    Console.WriteLine(v.ProductName + ": " + v.Category);
}
like image 183
xscape Avatar answered Oct 20 '22 17:10

xscape


Try this

var query2 = from c in db.YourTable
        join q in query1 on c.Id equals q.Id
        select c;

Where query1 wats your first collection that originated from a linq query.

like image 27
Raymund Avatar answered Oct 20 '22 19:10

Raymund