【发布时间】:2013-06-21 21:41:22
【问题描述】:
我有一个包含字符串子列表的对象列表,这些结构可以在几天之间更改,我希望比较它们以查看是否进行了更改。
public class Recipe
{
public string ID { get; set; }
public string Name { get; set; }
public List<string> Ingredients { get; set; }
}
ID 字段在列表版本之间是相同的,Ingredients 只是 字符串的列表。
List<Recipe> list1 = GetRecipes("2013-06-20");
List<Recipe> list2 = GetRecipes("2013-06-21");
我正在尝试查找所有在几天之间发生成分变化的Recipes。我已经能够想出一个 Linq 语句来找到新的 Recipes 是在 list2 但不是 list1 像这样
var newRecipes = list1.Where(x => !list2.Any(x1 => x1.ID == x.ID))
.Union(list2.Where(x => !list1.Any(x1 => x1.ID == x.ID)));
但是,我还没有弄清楚如何只选择列表之间 Ingredient 发生变化的 Recipes。
var modifiedRecipes = list1.Where(x => !list2.Any(x1 => x1.ID == x.ID && x1.Ingedients.SequenceEqual(x.Ingedients)))
.Union(list2.Where(x => !list1.Any(x1 => x1.ID == x.ID && x1.Ingedients.SequenceEqual(x.Ingedients))));
如何获取在字符串子列表中发生更改的对象列表?
【问题讨论】: