单排序C#列表中用不同的类型?中用、不同、类型、列表

2023-09-07 09:37:14 作者:一颗心在瞬间死掉

我是新来的C#! :)我使用.NET4 VS2010。

我有三个班,每班一个用于构建该类型的对象的列表。所有这三个继承形成一个基类。

欲得到的三个列表结合使用至一个和排序它们的基类的元素之一。

这个问题能与不同类型的列表呢?

简单的例子:

在创建每个列表

 公开名单< TestOne> TestOne列表;
公开名单< TestTwo> TestTwoList;
公开名单<对象> BothLists;
 

$ C $三来一补TestOne和TestTwo ...

什么/我如何结合这两种TestOne和TestTwo到BothLists和排序他们SeqNumber ???

 公共类BaseClassTest
{
    公共字符串禄{获得;组; } //禄
    //序列号由将在所得类被分配命令
    公众诠释SeqNumber {获得;组; }
}

公共类TestOne:BaseClassTest
{
    公众诠释编号{获得;组; }
}

公共类TestTwo:BaseClassTest
{
    公共字符串CatName {获得;组; }
}
 

解决方案 怎样在EXCEL中的分类汇总分便升降序

您应该能够做到:

 名单,其中,BaseClassTest>排序= TestOneList.Cast< BaseClassTest>()
                    .Union(TestTwoList.Cast&其中; BaseClassTest>())
                    .OrderBy(项目=> item.SeqNumber)
                    .ToList();
 

这会做你的排序+联盟的一次。

I'm new to c#! :) i'm using .Net4 VS2010.

I have Three classes each one is used to build a list of objects of that type. All three inherit form a base class.

I want to combine the resulting three lists in to one and sort them on one of the base class elements.

Can this be done with lists of different types?

Simplified Example:

Each list is created

public List<TestOne> TestOne list;
public List<TestTwo> TestTwoList;
public List<object> BothLists;

Code to fill TestOne and TestTwo…

What/How do I combine both TestOne and TestTwo into BothLists and sort them on SeqNumber???

public class BaseClassTest 
{
    public string Loc { get; set; }  // loc
    // sequence number to order by will be assigned in the resulting class
    public int SeqNumber { get; set; } 
}

public class TestOne : BaseClassTest
{
    public int Number { get; set; } 
} 

public class TestTwo : BaseClassTest
{
    public string CatName { get; set; } 
} 

解决方案

You should be able to do:

List<BaseClassTest> sorted = TestOneList.Cast<BaseClassTest>()
                    .Union(TestTwoList.Cast<BaseClassTest>())
                    .OrderBy(item => item.SeqNumber)
                    .ToList();

This will do your sort + union all at once.