【发布时间】:2017-03-03 10:58:29
【问题描述】:
我有一个对象如下图:
public class School
{
public string schoolName { get;set;}
public List<ClassRoom> classAttribute{get;set;}
}
public class ClassRoom
{
public string Duster{get;set;}
public string BlackBoard {get;set;}
public List<Students> students {get;set;}
}
public class Students
{
public int StudentID {get;set;}
public string StudentName {get ;set;}
public string Weight {get;set;}
public string Height {get;set;}
}
我还有一个Type <key,CustomObject> 的dict,其中CustomObject 包含所有"Students" 类字段以及其他字段。
public class CustomObject
{
public string HairColor {get;set;}
public int StudentID {get;set;}
public string StudentName {get ;set;}
public string Weight {get;set;}
public string Height {get;set;}
public string Ethnicity {get;set;}
}
我的字典是这样构建的,key 是 StudentName,Value 是 CustomObject
喜欢
dict<Key,CustomObject> dict
QuickWatch
Key : Student1
Value: [0] :
HairColor : Black
StudentID : 1
StudentName : Student1
Weight : 58
Height : 89
Ethnicity : Asian
[1] :
HairColor : Brown
StudentID : 2
StudentName : Student1
Weight : 24
Height : 22
Ethnicity : Arfican
我有一个 School 对象,其中学生列表
List<Students> studentList = new List<Students>();
在内部Students 对象中只有“StudentName”,其他字段为空。我必须在字典中找到这个StudentName
并从 "CustomObject" 构建 "Students" 对象,然后将其附加到 "ClassRoom" 。因此,当我立即展开 School 对象时,最后我应该能够为 Student 对象填充所有字段(上面示例中的 2 个学生对象,一个 HairColor 为黑色,一个 Haircolor 为棕色)窗口并导航到“School”,数据来自字典。
。在示例中,一个学生拥有 2 个 PropertyObject 只是假设。
我将如何继续获得结果。是不是有点像:
studentList.Select(x => x.classAttribute)
.Select(y => y.students)
.Where(z=>z.StudentName == dict.key);
但问题是我必须在附加之前从CustomObject 构建Student 对象。那么,我如何使用 linq 呢?
【问题讨论】:
标签: c# .net linq dictionary