linq连接表与集合

时间:2010-11-17 00:17:34

标签: linq

我有一个我从linq查询收集的集合,我想将该集合用作另一个查询的连接。我该怎么做?

由于

5 个答案:

答案 0 :(得分:6)

<强> LINQ 101 Samples

here

复制LINQ语句
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);
}

答案 1 :(得分:1)

试试这个

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

其中query1会丢弃源自linq查询的第一个集合。

答案 2 :(得分:0)

你应该可以这样做:

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

答案 3 :(得分:0)

下面是我写的Linq查询,它正是这样做的。这是一个连接到本地集合的Linq to SQL查询。

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

答案 4 :(得分:0)

...并未提供大量信息,但这是一个非常简单的例子。

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