我有数据库模型
public class Dish
{
public int Id { get; set; }
public String Name { get; set; }
public Dictionary<Ingridient, int> Ingridients { get; set; }
public List<String> Images { get; set; }
public String Guide { get; set; }
public Guid User { get; set; }
public DishType dishType { get; set; }
public int Rate { get; set; }
public enum DishType
{
Polish,
Asian,
Indian,
Other
};
}我想得到所有的菜,有梯度,要求从List<Ingridient>。有点像
输入List<Ingridient>
把字典中包含所有这些渐变的菜都拿出来。而不听话是关键。
我尝试了一些缓慢的解决方案,它适用于数据库中的所有菜品,但我想移到,以使它更快。
我的解决方案--非常慢--占用了大量的记忆:
List<Dish> allDishes = Dishes.Select(n => n).ToList();
List<Dish> Found = new List<Dish>();
foreach(var d in allDishes)
{
List<Ingridient> fromDish = d.Ingridients.Keys.ToList();
foreach(var ing in ingridients)
{
if (!fromDish.Contains(ing))
break;
}
Found.Add(d);
}我想将此行为重新创建为Linq查询。
发布于 2017-10-24 16:14:51
试一试
dishes.Where(dish => !ingredients.Except(dish.Ingredients).Any()).ToList();其中,!ingredients.Except(dish.Ingredients).Any()检查请求列表中的所有成分是否包含在菜肴的配料中。
你可以摆弄一个测试的这里。
https://stackoverflow.com/questions/46904910
复制相似问题