我使用Entity Framework创建了多对多的关系。
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中有肉和蔬菜的动物列表。
修改
到目前为止,我尝试了一些事情。
首先,我在ViewModel
中有这个 denFoodList = new List<Food>();
//Check if myDen requires any Food.
denFoodList = MyDen.FoodList.ToList();
我尝试循环遍历DenFoodList的每个成员并将动物添加到动物列表中然后收集列表x中的任何动物x次(其中x是FoodList.Count())是我需要的动物,但是这种方法很慢而且很乱。
我尝试将All方法与动物列表和DenList一起使用,但我无法使其工作。
animalList = context.Animals.Where(a => a.FoodList.All(f => f.DenList));
我一直在研究使用连接和交叉,但我还没有成功使用它们来解决这个问题。
编辑结束
感谢任何帮助。 感谢。
答案 0 :(得分:0)
未测试:
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();
}
}
答案 1 :(得分:0)
我们试试这个
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;
}
答案 2 :(得分:-1)
首先获取食物清单,然后获取动物:
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);
根据您的数据,您可能希望使用词典而不是列表,这取决于您的数据。
或许您正在寻找类似的东西?
Dan d = SomeDen();
var food = d.FoodList;
var animals = from a in DB.Animals
where a.FoodList.Any((f)=>food.Contains(f))
select a;
后者应该是你直观的想法,但它会很慢。