[英]Add results of 2 queries into a single list and then sort it
在我的 model 中,我有 2 個類: ParticipantCompany
和ParticipantPerson
都繼承自Participant
並具有字符串屬性.Name
。 他們也都意識到IParticipant
要求他們擁有.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>();
}
這是 model 本身:
class CaseContext : DbContext
{
public DbSet<ParticipantCompany> Companies{ get; set; }
public DbSet<ParticipantPerson> Persons { get; set; }
public DbSet<LegalCase> Cases { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=Clients.db");
}
我想要做的是查詢所有Company
和所有Person
,將結果合並到一個列表中,然后按.Name
對列表進行排序。
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}));
或者我們也可以創建一個具有上述所有匿名類型屬性的新 class 並使用該 class 的 object 代替 ZE0626222C16144BEE3 查詢中的匿名類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.