C# 将两个查询的结果添加到单个列表中,然后对其进行排序

C# 将两个查询的结果添加到单个列表中,然后对其进行排序,c#,list,entity-framework-core-5,C#,List,Entity Framework Core 5,在我的模型中,我有两个类:participantcompant和ParticipantPerson,它们都继承自Participant,并具有字符串属性。Name。他们还都意识到,ipparticipant要求他们拥有.Name public interface IParticipant { public string Name { get; set; } } public class ParticipantCompany : Participant, IParticipant {

在我的模型中,我有两个类:
participantcompant
ParticipantPerson
,它们都继承自
Participant
,并具有字符串属性
。Name
。他们还都意识到,
ipparticipant
要求他们拥有
.Name

public interface IParticipant 
{
    public string Name { get; set; }
}

public class ParticipantCompany : Participant, IParticipant
{
    public ParticipantCompany():this(false, "","","","") { }
    public ParticipantCompany (bool isclient) : this(isclient, "","","","") { }
    public ParticipantCompany(bool isclient, string name, string address, string inncompany, string ogrn) : base(isclient, SubjectType.Company)
    {
        Name = name;
        Address = address;
        InnCompany = inncompany;
        Ogrn = ogrn;
    }
    public string InnCompany { get; set; }
    public string Ogrn { get; set; }
}
public class ParticipantPerson : Participant, IParticipant
{
    public ParticipantPerson() : this(false,"","","","") { }
    public ParticipantPerson(bool isclient) : this(isclient, "", "", "", "") { }
    public ParticipantPerson(bool isclient, string name, string address, string innperson, string ogrnip) : base(isclient, SubjectType.Person) 
    {
        Name = name;
        Address = address;
        InnPerson = innperson;
        Ogrnip = ogrnip;
    }
public string InnPerson { get; set; }
    public string Ogrnip { get; set; }
}
public abstract class Participant
{
    public Participant(bool isclient, SubjectType Type,  string name, string address) 
    { 
        SubjType = Type;
        IsClient = isclient;
        Name = name;
        Address = address;
    }

    public Participant(bool isclient, SubjectType Type ) : this(isclient, Type, "","")
    {

    }
    public int Id { get; set; }
    public  SubjectType SubjType { get; private set; }
    public bool IsClient { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public List<LegalCase> Cases { get; set; } = new List<LegalCase>();

}

我们可以将所有公司和个人合并到一个列表中,如下所示:

//store actual data in below two variables
List<ParticipantPerson> participantPersons = new List<ParticipantPerson>();
List<ParticipantCompany> participantCompanies = new List<ParticipantCompany>();

//Merge them using Linq and anonymous types
var mergedList = participantPersons.Select(pp => new { Id = pp.Id, SubjType = pp.SubjType, IsClient = pp.IsClient, Name = pp.Name, Address = pp.Address,  InnPerson = pp.InnPerson, Ogrnip = pp.Ogrnip, InnCompany = (string)null, Ogrn = (string)null }).ToList();
mergedList.AddRange(participantCompanies.Select(pc => new {Id = pc.Id, SubjType = pc.SubjType, IsClient = pc.IsClient, Name = pc.Name, Address = pc.Address, InnPerson = (string) null, Ogrnip = (string) null, InnCompany = pc.InnCompany, Ogrn = pc.Ogrn}));
        
//将实际数据存储在以下两个变量中
List participantPersons=新列表();
列表参与者公司=新列表();
//使用Linq和匿名类型合并它们
var mergedList=participantperson.Select(pp=>new{Id=pp.Id,subtype=pp.subtype,IsClient=pp.IsClient,Name=pp.Name,Address=pp.Address,InnPerson=pp.InnPerson,Ogrnip=pp.Ogrnip,InnCompany=(string)null,Ogrn=(string)null});
mergedList.AddRange(ParticipantCompanys.Select(pc=>new{Id=pc.Id,SubType=pc.SubType,IsClient=pc.IsClient,Name=pc.Name,Address=pc.Address,InnPerson=(string)null,Ogrnip=(string)null,InnPCompany=pc.InnCompany,Ogrn=pc.Ogrn});

或者,我们也可以创建一个具有上述所有匿名类型属性的新类,并在Select query中使用该类的对象而不是匿名类型。

如果不进行强制转换,您将无法执行此操作,因为类型不同。可能类似于
companys.Cast().Concat(persons.Cast()).OrderBy(x=>x.Name)
?但是,请注意,您丢失了类型,剩下的是
ipparticipant
,但您没有提到这是否可以接受。就用户界面而言,我希望公司和个人混合在一起(比如将他们的名字放在单个列表框上)我不认为把它们放在一个单一的列表中是一个困难的要求,你会建议不要这样做吗?如果需要的话,我可以做一个名字的列表。如果参与者的名字是通过JSON返回的,那么我上面的评论可能就是你想要的,但是UI是一个非常广泛的术语,你想把所有的公司和个人都放在一个什么的列表中?它们是两种不同的东西。可以将它们转换为
参与者
IParticpant
。您正在寻找的最终结果并不清楚。我的意思是在使用WPF创建的GUI中编辑数据库提供的数据
        using(var db = new CaseContext())
        {
            var companies = db.Companies.ToList();
            var persons = db.Persons.ToList();
            //Cant convert from System.Collections.
            //Generic.List<Magna.CaseModel.ParticipantPerson> to 
            //System.Colleciton.Generic.IEnumerable<Magna.CaseModel.ParticipantCompany>
            List<IParticipant> participants = companies.AddRange(persons);

        }   
//store actual data in below two variables
List<ParticipantPerson> participantPersons = new List<ParticipantPerson>();
List<ParticipantCompany> participantCompanies = new List<ParticipantCompany>();

//Merge them using Linq and anonymous types
var mergedList = participantPersons.Select(pp => new { Id = pp.Id, SubjType = pp.SubjType, IsClient = pp.IsClient, Name = pp.Name, Address = pp.Address,  InnPerson = pp.InnPerson, Ogrnip = pp.Ogrnip, InnCompany = (string)null, Ogrn = (string)null }).ToList();
mergedList.AddRange(participantCompanies.Select(pc => new {Id = pc.Id, SubjType = pc.SubjType, IsClient = pc.IsClient, Name = pc.Name, Address = pc.Address, InnPerson = (string) null, Ogrnip = (string) null, InnCompany = pc.InnCompany, Ogrn = pc.Ogrn}));