如何对两种不同类型列表的对象进行分组和排序?
How to to grouping and ordering on an object of two different types of list?
我有一个包含两种不同类型列表的对象,我只想对该特定对象执行 OrderBy
和 GroupBy
。
我不想为两个列表分别做。
现在我正在这样做:
List<object> combinePlacePerson = (from x in recipientFilteredDataByPersons.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]) select (object)x).ToList();
combinePlacePerson.AddRange((from x in recipientFilteredDataByPlaces.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]) select (object)x).ToList());
cvrbyperson.Source = combinePlacePerson;
cvrbyperson
是一个 CollectionViewSource
对象。
但我想在组合对象上进行。
像这样创建一个界面:
public interface IHaveFirstName
{
string FirstName { get; set; }
}
从这个接口
继承你的类里面的属性recipientFilteredDataByPersons
recipientFilteredDataByPlaces
现在您可以这样做了:
List<IHaveFirstName> combinePlacePerson = (from x in recipientFilteredDataByPersons
select (IHaveFirstName)x).ToList(); //you need inheritance here
combinePlacePerson.AddRange((from x in recipientFilteredDataByPlaces
select (IHaveFirstName)x).ToList()); //and here
//here you order full collection
combinePlacePerson = combinePlacePerson.OrderBy(x => x.FirstName)
.GroupBy(x => x.FirstName[0]);
我有一个包含两种不同类型列表的对象,我只想对该特定对象执行 OrderBy
和 GroupBy
。
我不想为两个列表分别做。
现在我正在这样做:
List<object> combinePlacePerson = (from x in recipientFilteredDataByPersons.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]) select (object)x).ToList();
combinePlacePerson.AddRange((from x in recipientFilteredDataByPlaces.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]) select (object)x).ToList());
cvrbyperson.Source = combinePlacePerson;
cvrbyperson
是一个 CollectionViewSource
对象。
但我想在组合对象上进行。
像这样创建一个界面:
public interface IHaveFirstName
{
string FirstName { get; set; }
}
从这个接口
继承你的类里面的属性recipientFilteredDataByPersons
recipientFilteredDataByPlaces
现在您可以这样做了:
List<IHaveFirstName> combinePlacePerson = (from x in recipientFilteredDataByPersons
select (IHaveFirstName)x).ToList(); //you need inheritance here
combinePlacePerson.AddRange((from x in recipientFilteredDataByPlaces
select (IHaveFirstName)x).ToList()); //and here
//here you order full collection
combinePlacePerson = combinePlacePerson.OrderBy(x => x.FirstName)
.GroupBy(x => x.FirstName[0]);