簡體   English   中英

LINQ選擇列表中的所有其他類型列表中的項目

[英]LINQ select items in a list that are all in a list of another type

我有使用實體框架創建的多對多關系。

public class Animal
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int AnimalID { get; set; }
    [MaxLength(50)]
    public string AnimalName { get; set; }

    public virtual ICollection<Food> FoodList { get; set; }

}


public class Den
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int DenID { get; set; }
    [MaxLength(50)]
    public string DenName { get; set; }

    public virtual ICollection<Food> FoodList { get; set; }
}

Animal和Den都包含類型食物的虛擬列表。

public class Food
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int FoodID { get; set; }
    [MaxLength(50)]
    public string FoodName { get; set; }
    public virtual ICollection<Animal> AnimalList { get; set; }
    public virtual ICollection<Den> DenList { get; set; }
}

我有一個傳遞DenID的方法,我需要返回一個動物列表,其中所有動物的Animal.FoodList中都包含Den的Den.FoodList。

例如,如果Den.FoodList包含Meat和Veg,則我想要一個在foodList中包含Meat和Veg的動物的列表。

編輯

到目前為止,我已經嘗試了一些方法。

首先,我在ViewModel中有這個

        denFoodList = new List<Food>();

        //Check if myDen requires any Food.
        denFoodList  = MyDen.FoodList.ToList();

我嘗試遍歷DenFoodList的每個成員,並將Animals添加到Animal列表中,然后收集列表中的任何動物x次(其中x是FoodList.Count())是我需要的動物,但是此方法是緩慢而混亂。

我嘗試將All方法與動物列表和DenList一起使用,但無法正常工作。

animalList = context.Animals.Where(a => a.FoodList.All(f => f.DenList));

我一直在研究使用連接和相交,但在使用它們來解決此問題方面還沒有成功。

編輯結束

任何幫助表示贊賞。 謝謝。

未經測試:

class Test
{
    private static IEnumerable<Den> Dens()
    {
        var dens = new List<Den>
        {
            new Den
            {
                DenID = 1,
                DenName = "GamePark",
                FoodList = new Collection<Food>()
                {
                    new Food
                    {
                        FoodID = 1,
                        FoodName = "Veg",
                        AnimalList = new Collection<Animal>
                        {
                            new Animal
                            {
                                AnimalID = 234,
                                AnimalName = "Zebra",
                                FoodList = new Collection<Food>{new Food {FoodID = 1, FoodName = "Veg"} }
                            },
                            new Animal
                            {
                                AnimalID = 125,
                                AnimalName = "Buffalo",
                                FoodList = new Collection<Food>{new Food {FoodID = 1, FoodName = "Veg"} }
                            }
                        }
                    },
                    new Food
                    {
                        FoodID = 2,
                        FoodName = "Meat",
                        AnimalList = new Collection<Animal>
                        {
                            new Animal
                            {
                                AnimalID = 003,
                                AnimalName = "Leopard",
                                FoodList = new Collection<Food>{new Food {FoodID = 2, FoodName = "Meat"} }
                            },
                            new Animal
                            {
                                AnimalID = 001,
                                AnimalName = "Lion",
                                FoodList = new Collection<Food>{new Food {FoodID = 2, FoodName = "Meat"} }
                            }
                        }
                    }
                }
            }
        };

        return dens;
    }

    public static IEnumerable<Animal> GetAnimalsWithFoodsInDen(int denId)
    {

        var den = Dens().FirstOrDefault(x => x.DenID == denId);
        var animals = new List<Animal>();
        if (den != null)
        {
            var foods = den.FoodList;
            if (foods != null)
            {
                animals = foods.ToList().Aggregate(animals, (current, food) => current.Union(food.AnimalList).ToList());
            }
        }

        return animals;
    }

    static void Main(string[] args)
    {
        var result = GetAnimalsWithFoodsInDen(1);

        foreach (var a in result)
        {
            Console.WriteLine(a.AnimalName);
        }

        Console.ReadLine();

    }
}

讓我們嘗試一下

class MyContext : DbContext {}

// ...
using (MyContext context = new MyContext())
{
   var den = context.Den.Find(DenId);
   // Inner join Linq
   var foodList = from a in context.Animals
                  from b in a.FoodList
                  join c in d.FoodList on c.FoodId equals b.FoodId
                  select c;
}

首先獲取食物清單,然后獲取動物:

Den d = SomeDen();
var food = d.FoodList;
var animals = new List<Animal>();
foreach(var f in food) foreach(var a in f.AnimalList) if(!animals.Contains(a)) animals.Add(a);

也許您希望使用字典而不是List來提高性能,具體取決於您的數據。

還是您正在尋找類似的東西?

Dan d = SomeDen();
var food = d.FoodList;
var animals = from a in DB.Animals
              where a.FoodList.Any((f)=>food.Contains(f))
              select a;

后者應該是您的直覺主意,但是會很慢。

暫無
暫無

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

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