2017-06-14 75 views
-2

我有两种不同类型的列表的对象,我只想对该特定对象执行OrderByGroupBy如何对两种不同类型的列表进行分组和排序?

我不想为这两个列表单独做它。

现在我这样做:

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; 

cvrbypersonCollectionViewSource对象。

但我想在组合的对象上做到这一点。

+0

如何创建recipientFilteredDataByPersons和recipientFilteredDataByPlaces?他们是什么类型的名单?它们的对象有一些共同的特性? –

回答

0

创建这样一个接口:

public interface IHaveFirstName 
{ 
    string FirstName { get; set; } 
} 

继承你的类里面的属性recipientFilteredDataByPersonsrecipientFilteredDataByPlaces从这个接口

现在你能做到这一点:

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]); 
相关问题