将 2 个查询的结果添加到一个列表中,然后对其进行排序
Add results of 2 queries into a single list and then sort it
在我的模型中,我有 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>();
}
这是模型本身:
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 具有上述所有匿名类型的属性,并在 Select 查询中使用该 class 的对象而不是匿名类型。
在我的模型中,我有 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>();
}
这是模型本身:
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 具有上述所有匿名类型的属性,并在 Select 查询中使用该 class 的对象而不是匿名类型。