C# LINQ选择列表中的项目,这些项目都在另一种类型的列表中
我使用实体框架创建了多对多关系C# LINQ选择列表中的项目,这些项目都在另一种类型的列表中,c#,linq,entity-framework,list,C#,Linq,Entity Framework,List,我使用实体框架创建了多对多关系 public class Animal { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int AnimalID { get; set; } [MaxLength(50)] public string AnimalName { get; set; } public virtual ICollection<
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; }
}
我一直在研究使用连接和相交,但我还没有成功地使用它们来解决这个问题
编辑结束
感谢您的帮助。
谢谢。先拿食物清单,然后再拿动物:
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);
后者应该是你的直觉想法,但它会非常缓慢。未经测试:
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();
}
}
类测试
{
私有静态IEnumerable Dens()
{
var dens=新列表
{
新巢穴
{
丹尼德=1,
DenName=“游戏公园”,
FoodList=新集合()
{
新食品
{
FoodID=1,
FoodName=“Veg”,
AnimalList=新收藏
{
新动物
{
AnimalID=234,
AnimalName=“斑马”,
FoodList=new Collection{new Food{FoodID=1,FoodName=“Veg”}
},
新动物
{
AnimalID=125,
AnimalName=“布法罗”,
FoodList=new Collection{new Food{FoodID=1,FoodName=“Veg”}
}
}
},
新食品
{
FoodID=2,
FoodName=“肉类”,
AnimalList=新收藏
{
新动物
{
AnimalID=003,
AnimalName=“Leopard”,
FoodList=newcollection{newfood{FoodID=2,FoodName=“Meat”}
},
新动物
{
AnimalID=001,
AnimalName=“狮子”,
FoodList=newcollection{newfood{FoodID=2,FoodName=“Meat”}
}
}
}
}
}
};
返回巢穴;
}
公共静态IEnumerable GetAnimals with FoodInden(int denId)
{
var den=Dens().FirstOrDefault(x=>x.DenID==DenID);
var animals=新列表();
如果(den!=null)
{
var foods=den.FoodList;
如果(食物!=null)
{
动物=食品.ToList().Aggregate(动物,(当前,食品)=>current.Union(食品.AnimalList).ToList());
}
}
归还动物;
}
静态void Main(字符串[]参数)
{
var结果=GetAnimalsWithFoodInden(1);
foreach(结果中的var a)
{
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);
Dan d = SomeDen();
var food = d.FoodList;
var animals = from a in DB.Animals
where a.FoodList.Any((f)=>food.Contains(f))
select a;
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;
}