开发者

linq join table with collection

开发者 https://www.devze.com 2023-01-25 17:02 出处:网络
I have a collection that I\'m gathering from a linq query and 开发者_StackOverflow社区I\'d like to use that collection as a join for another query. How do I do that?

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

Thanks


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);
}


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.


You should be able to do this:

 List<Product> products = GetProductList(); //collection

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


Below is a Linq query I wrote that does exactly that. This is a Linq to SQL query that joins in a local collection.

int[] keyList = new int[] { 7064, 7065, 6242 };

var query = (from child in ETAModule
        join parent in ETA on child.ExpID equals parent.ExpID
        where child.ID >= 65490442
        where keyList.Contains(child.ExpID)
        orderby child.ID
        select new { EtaModule = child, Eta = parent });


...not a whole lot of information provided, but here is a very simple example.

var firstCollection = List<YourClassNameHere>(); // this is your pre-existing collection
var results = from o in firstCollection
               join i in SomeOtherCollection on o.JoinField equals i.JoinField
               select i; // or whatever you like
0

精彩评论

暂无评论...
验证码 换一张
取 消