如何为实体框架中的所有子节点使用条件

本文关键字:子节点 条件 实体 框架 | 更新日期: 2023-09-27 18:13:52

我有这些类:

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"的所有商店。我想在实体框架中做这个,而不是在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(); 

更新:无望的答案是(最佳答案):

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