将2个可观察集合中的匹配值组合到第3个[WPF]中 [英] Combining matching values in 2 observablecollections into a 3rd [WPF]
本文介绍了将2个可观察集合中的匹配值组合到第3个[WPF]中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,所以我有一个看起来像这样的 ObservableCollection
:
Hello so I have one ObservableCollection
that looks like this:
new time { Arrival = stringlist[0], Departure = stringList[1]};
new time2 { Arrived = stringlist[0], Departed = stringList[1]};
我想创建一个名为 datagridTime
的新 ObservableCollection
,并仅将匹配的记录插入此 ObservableCollection
中,所以说我们有>
I would like to make a new ObservableCollection
called datagridTime
and insert into this ObservableCollection
only records that match so lets say we have
Time `ObservableCollection`
Arrival | Departure
---------------------------------
10 20
10 30
10 10
Time2 `ObservableCollection`
Arrival | Departure
---------------------------------
10 20
10 30
10 20
datagridTime `ObservableCollection`
Arrived | Departed
---------------------------------
10 20
10 30
推荐答案
这是一个简短的示例应用程序,可以完成您想要的操作.
Here is a short sample App which should do what you want.
魔术部分是这样:
public ObservableCollection<Person> People1 { get; } = new ObservableCollection<Person>()
{
new Person("Donald", "Duck"),
new Person("Daisy", "Duck"),
new Person("Jack", "Daniels")
};
public ObservableCollection<Person> People2 { get; } = new ObservableCollection<Person>()
{
new Person("Donald", "Duck"),
new Person("Daisy", "Duck"),
new Person("Jim", "Beam")
};
public IEnumerable<Person> PeopleInBothCollections
{
get
{
foreach (var person in People1)
{
if (People2.Any(x => x.FirstName == person.FirstName && x.LastName == person.LastName))
{
yield return person;
}
}
}
}
无论何时引发 PropertyChanged
事件,它都会更新您的第三个列表:
When ever you raise the PropertyChanged
-event it should update your third list:
这是示例的链接: 查看全文