问题
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]};
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
回答1:
Here is a short sample App which should do what you want.
The magic part is this:
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;
}
}
}
}
When ever you raise the PropertyChanged
-event it should update your third list:
And here is the link to the example: https://github.com/timunie/MahApps.Metro.Examples/tree/master/src/MahApps.Metro.Examples/IntersectTwoLists
回答2:
You can use Intersect()
Linq method, but you will have to implemente an EqualityComparer on your Time
objects
Here you have the docs: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.intersect?view=netcore-3.1
回答3:
This should work:
var time1 = new { Arrived = new List<string>(),
Departure = new List<string>() };
var time2 = new { Arrived = new List<string>(),
Departure = new List<string>() };
var time3 = new{ Arrived = time1.Arrived.Intersect(time2.Arrived),
Departure = time1.Departure.Intersect(time2.Departure) };
回答4:
Join can be used to solve this. This a solution for joining two collections based on some condition. In Your case u can replace the left and right anonymous collection with ObservableCollection
var left = new[] {
new { Arrival = 10, Departure = 20 },
new { Arrival = 10, Departure = 30 },
new { Arrival = 10, Departure = 10 }
};
//This collection contains duplicates hence needs to be removed either before or later joining the collection.
var right = new[] {
new { Arrival = 10, Departure = 20 },
new { Arrival = 10, Departure = 30 },
new { Arrival = 10, Departure = 20 }
};
var result =
left.Distinct() //remove duplicates
.Join(right.Distinct(), // Remove duplicates
right => new { right.Arrival, right.Departure },
left => new { left.Arrival, left.Departure },
(l, r) => new { r, l } // Matching elements in both collections use can return either of noth
).ToList();//To list is called to execute the query and get result
Urge to check out more here
来源:https://stackoverflow.com/questions/62412876/combining-matching-values-in-2-observablecollections-into-a-3rd-wpf