2016-05-20 9 views
0

Ich habe nicht versucht, den Join zu verwenden, aber heute möchte ich eine komplizierte Situation mit Join beheben, ich habe eine Daten, die unten in Beispiel aufgeführt ist. Ich habe auch einen Kommentar abgegeben. Wie wir weiter vorgehen?Join auf Liste der Liste in C#

namespace JoinsApplication 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      List<Category> categories = new List<Category>() 
      { 
       new Category(){Name="Beverages", ID=001,Products =new List<Product>(){new Product{Name="Cola", CategoryID=001},new Product{Name="Tea", CategoryID=001} } }, 
       new Category(){ Name="Condiments", ID=002,Products =new List<Product>(){new Product{Name="Mustard", CategoryID=002},new Product{Name="Pickles", CategoryID=003} } }, 
       new Category(){ Name="Vegetables", ID=003,Products =new List<Product>(){new Product{Name="Carrots", CategoryID=003},new Product{Name="Melons", CategoryID=003} } }, 
       new Category() { Name="Grains", ID=004,Products =new List<Product>(){new Product{Name="Carrots", CategoryID=003},new Product{Name="Melons", CategoryID=003} } }, 
       new Category() { Name="Fruit", ID=005,Products =new List<Product>(){new Product{Name="Peaches", CategoryID=005},new Product{Name="Melons", CategoryID=005} } }  
      }; 

      //filter data using join, i want to filter data and want a condition on cat.ID and Product.CategoryID, but Products property is not accessible using category alias. 
      //var u= from cat in categories join product in cat.Products on product.CategoryID equels cat.ID select cat.Name, product.Name; 
     } 
    } 

    #region Join Demonstration data 

    public class Product 
    { 
     public string Name { get; set; } 
     public int CategoryID { get; set; } 
    } 

    public class Category 
    { 
     public string Name { get; set; } 
     public int ID { get; set; } 
     public List<Product> Products { get; set; } 
    } 

    // Specify the first data source. 


    #endregion 

} 
+1

Können Sie ein Beispiel für die erwartete ou geben tput? –

Antwort

2

Einfach.

var uu = (from cat in categories 
      join product in categories.SelectMany(p => p.Products) 
      on cat.ID equals product.CategoryID 
      select new 
      { 
       CategoryName = cat.Name, 
       ProductName = product.Name 
      }).ToList(); 

Edit (Verwenden von Lambda): Sie haben soeben eine vollständige Liste der Produkte benötigen, dieses zu beenden, kann dies SelectMany mit erreicht werden

var uuLambda = categories.Join(categories.SelectMany(p => p.Products), 
           cat => cat.ID, prod => prod.CategoryID, 
           (cat, prod) => new { CategoryName = cat.Name, ProductName = prod.Name }).ToList(); 

unten auf Ihrem Kommentar-basiert sind, ist keine gute Idee, und es ist nicht lesbar:

var uuWhere = categories.SelectMany(p => p.Products). 
         Select(prod => new { ProductName = prod.Name, 
               CategoryName = categories.Where(x => x.ID == prod.CategoryID). 
                  Select(x => x.Name).FirstOrDefault() }).ToList(); 
+0

Danke, Lösung funktioniert nach meinen Erwartungen. –

+0

Wie ist es möglich, Lambda-Ausdruck zu verwenden? Bitte antworten Sie, wenn Sie eine Idee haben. –

+0

Sie meinen, ohne die "von innen" -Syntax? – user3185569