简体   繁体   中英

Compare 2 observable collections and add to a third?

I have two observable collections, one is a fullteam the other is matchdayteam . Is it possible to compare them and add any different entries on fullteam to another observable collection?

The current answer is close, but no cigar. I fully answered the question in the comments for the first answer, but thought I would put it here for more visibility:

var fullteam = new ObservableCollection<string> { "John", "Mike", "Steve" };
var matchdayteam = new ObservableCollection<string> { "Peter", "Mike", "Jacob" };
var combined = new ObservableCollection<string>(fullteam.Union(matchdayteam));

// combined = John, Mike, Steve, Peter, Jacob

Not entirely sure i understand what you mean but i believe you could use a where statement like:

var x = (ObservableCollection<string>) fullteam.Where(i => !matchdayteam.Contains(i));

like so:

var fullteam = new ObservableCollection<string>();
var matchdayteam = new ObservableCollection<string>();

fullteam.Add("one");
fullteam.Add("two");
matchdayteam.Add("one");

var x = (ObservableCollection<string>) fullteam.Where(i => !matchdayteam.Contains(i));

This should give you x which contains only the things in fullteam that is not in matchdayteam

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM