简体   繁体   中英

How to use condition for all children in Entity Framework

I have these classes:

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

And I have this list :

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

I want to select all stores where Name ="Lilo" and products names is "Dell " and Color="Blue". I want do this in Entity Framework, not Linq.

I use this code but it doesn't work :

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

How can I do this ?

Do this By Method Syntax :

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

Updated : And Hopeless 's Answers is (best answers):

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

And by 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM