C# 如何对两种不同类型列表的对象进行分组和排序?
我有一个具有两种不同类型列表的对象,我只想对该特定对象执行C# 如何对两种不同类型列表的对象进行分组和排序?,c#,list,linq,sql-order-by,grouping,C#,List,Linq,Sql Order By,Grouping,我有一个具有两种不同类型列表的对象,我只想对该特定对象执行OrderBy和GroupBy 我不想为两个列表分别做这件事 现在,我正在这样做: List<object> combinePlacePerson = (from x in recipientFilteredDataByPersons.OrderBy(x => x.FirstName).GroupBy(x => x.FirstName[0]) select (object)x).ToList(); combineP
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;
List combinePlacePerson=(从RecipientFilteredataBypersons.OrderBy(x=>x.FirstName).GroupBy(x=>x.FirstName[0])中的x选择(对象)x.ToList();
combinePlacePerson.AddRange((从RecipientFilteredataByPlaces.OrderBy(x=>x.FirstName).GroupBy(x=>x.FirstName[0])中的x开始)选择(对象)x.ToList();
cvrbyperson.Source=combinePlacePerson;
cvrbyperson
是一个CollectionViewSource
对象
但我想在组合对象上执行此操作。创建如下界面:
public interface IHaveFirstName
{
string FirstName { get; set; }
}
从该接口继承属性RecipientFilteredDatatabypersons
RecipientFilteredDatatabyplaces
中包含的类
现在,您可以执行以下操作:
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]);
列出combinePlacePerson=(从RecipientFilteredTableTypersons中的x开始)
选择(IHaveFirstName)x.ToList()//你需要继承
combinePlacePerson.AddRange((从RecipientFilteredDataTablePlace中的x开始)
选择(IHaveFirstName)x.ToList()//这里呢
//这是您订购的全套
combinePlacePerson=combinePlacePerson.OrderBy(x=>x.FirstName)
.GroupBy(x=>x.FirstName[0]);
如何创建RecipientFilteredDatatabyperson和RecipientFilteredDatatabyplaces?它们是什么类型的列表?它们的对象有一些共同的属性吗?