0
我有以下情况,在这种情况下,我正在寻找拼合嵌套对象,并将父和子字段组合在一行中,而不是创建对象的父和子。嵌套类查询和加入
另外我想结合v1和v2。任何建议?
public class Person
{
public string idPerson;
public string names;
private List<Subject> subjects = new List<Subject>();
private List<MedicalRecord> medicalRecords = new List<MedicalRecord>();
}
public class Subject
{
public string subjectName;
public string subjectId;
}
public class MedicalRecord
{
public int recordId;
public string doctorName;
public string medicalCare;
}
void main()
{
/// Assume that Person is filled.
/// How can I join the two lists?
List<Person> all = new List<Person>();
var v1 = all.SelectMany(p => p.subjects, (child, parent) => new { child, parent });
var v2 = all.SelectMany(p => p.medicalRecords, (child, parent) => new { child, parent }); /// here i want to select all fields instead of objects to avoid child and parent calling from below cycle.
/// I want to join v1 and v2 and flatten the lists.
foreach(var obj in v1)
{
Console.WriteLine(obj.child.subjectName + " " + obj.parent.idPerson);
}
///
}
所以,你要与{人,主题,MedicalRecord},然而,一个人拥有多学科和医疗记录扁平列表结束,那么分组应该是什么样子?如果受试者多于医疗记录,反之亦然?有其他选择吗? –
我想加入并扁平al字段,这两个列表中的人员编号为 – jrpz
首先,考虑尝试'var v2 = all.medicalRecords',因为您无论如何都需要所有字段。为了将它们连接在一起,是否有什么特别的原因可以简单地在'v1.idPerson等于v2.idPerson'上使用linq的'join' –