簡體   English   中英

如何在Entity Framework中為所有子項使用條件

[英]How to use condition for all children in Entity Framework

我有這些課程:

public class product
{
    public int Id { get; set; }
    public string Title { get; set; }
    public Store Store { get; set; }

    public ICollection<Color> Colors { get; set; }
}

public class Color
{
    public int Id { get; set; }
    public string Name { get; set; }
    public product Product { get; set; }
}

public class Store
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }

    public ICollection<product> Products { get; set; }
}

我有這個清單:

  List<Store> Stores = new List<Store>
        {
            new Store { Id = 1, Name = "Lilo", City = "Teh",
                        Products = new List<product> 
                          {
                             new product 
                                 { Id = 1, Title = "Asus",
                                   Colors = new List<Color> {
                                        new Color { Id = 1, Name = "Blue"},
                                        new Color { Id = 2, Name = "Orange"}
                                    }
                                 },
                             new product 
                                 { Id = 2, Title = "Dell",
                                   Colors = new List<Color> {
                                        new Color { Id = 1, Name = "Yellow"},
                                        new Color { Id = 2, Name = "Orange"},
                                        new Color { Id = 3, Name = "Red"}
                                    }
                                 }
                }
            },
            new Store{Id=2,Name="filo",City="san",
                Products=new List<product> 
                {
                    new product{Id=3,Title="Asus",
                Colors=new List<Color>{
                    new Color{Id=1,Name="Blue"},
                    new Color{Id=2,Name="Orange"}
                }
            },
            new product{Id=4,Title="Dell",
                Colors=new List<Color>{
                    new Color{Id=1,Name="Yellow"},
                    new Color{Id=2,Name="Lime"},
                    new Color{Id=3,Name="Red"}
                }
            }
                }
            }
        };

我想選擇Name =“Lilo”的所有商店,產品名稱為“Dell”,Color =“Blue”。 我想在Entity Framework中這樣做,而不是Linq。

我使用此代碼,但它不起作用:

var test = Stores.Where(s => s.Name = "lilo" && s.Products.Where(p => p.Title == "Dell").FirstOrDefault().Title == "Dell" && s.Products.Where(c => c.Colors.Where(ct => ct.Name == "Blue").FirstOrDefault().Name = "Blue")).ToList();

我怎樣才能做到這一點 ?

按方法語法執行此操作:

var stlist = Stores.Where(s => s.Name.ToLower() == "lilo" && s.Products.Where(p => p.Colors.Any(c=>c.Name=="Blue") && p.Title == "Dell").FirstOrDefault().Title == "Dell").ToList(); 

更新:和Hopeless的答案是(最佳答案):

 var lslist2= Stores.Where(s => s.Name == "lilo" && s.Products.Any(p => p.Title == "Dell" && p.Colors.Any(c => c.Color.Name == "Blue"))).ToList();

並由Linq:

    var test = (from s in Stores
                   from p in s.Products
                   from c in p.Colors
                   where s.Name=="Lilo" && p.Title=="Dell"&& c.Name=="Blue" 
                   select s
                   ).ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM