我有这样一种情况,需要合并对象列表。列表中的每个对象都有一个属性,用于说明在合并器中应如何处理它。因此,请假设以下情况...
enum Cascade {
Full,
Unique,
Right,
Left
}
class Note {
int Id { get; set; }
Cascade Cascade { get; set; }
// lots of other data.
}
var list1 = new List<Note>{
new Note {
Id = 1,
Cascade.Full,
// data
},
new Note {
Id = 2,
Cascade.Right,
// data
}
};
var list2 = new List<Note>{
new Note {
Id = 1,
Cascade.Left,
// data
}
};
var list3 = new List<Note>{
new Note {
Id = 1,
Cascade.Unique,
// data similar to list1.Note[0]
}
}
那么,我将有一个方法...
Composite(this IList<IList<Note>> notes){
return new List<Note> {
notes.SelectMany(g => g).Where(g => g.Cascade == Cascade.All).ToList()
// Here is the problem...
.SelectMany(g => g).Where(g => g.Cascade == Cascade.Right)
.Select( // I want to do a _LastOrDefault_ )
// continuing for the other cascades.
}
}
这是我迷失的地方。我需要执行多个SelectMany
语句,但我不知道该如何做。但这是预期行为。
Cascade.Full
Note
将始终出现在最终集合中。
Cascade.Unique
Note
将只出现一次在最终集合中,忽略任何重复项。
Cascade.Left
Note
将出现在最终集合中,首个实例优先于后续实例。(因此,注释1、2、3是相同的。注释1被推送)
Cascade.Right
Note
将出现在最终集合中,最后一个实例优先于重复项。(因此,注释1、2、3是相同的。注释3被推送)