【问题标题】:Replacing an object in list with a list in C#用 C# 中的列表替换列表中的对象
【发布时间】: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 &lt;key,CustomObject&gt;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;}
}

我的字典是这样构建的,keyStudentNameValueCustomObject 喜欢

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


    【解决方案1】:

    我相信这就是您正在寻找的:

    var result = school
                    .classAttribute
                    .SelectMany(c => c.students)
                    .Where(s => s.StudentName == "some dict key")
                    .Select(s => new CustomObject
                    {
                        StudentID = s.StudentID,
                        StudentName = s.StudentName,
                        Height = s.Height,
                        Weight = s.Weight
                    }).Single();
    

    【讨论】:

      【解决方案2】:

      这可能会有所帮助

             school
                  .classAttribute
                  .SelectMany(ca=> ca.students)
                  .SelectMany(s=> s.students)
                  .ToList()
                  .ForEach(student =>
                  {
                      // Get data from dictionary and update properties
                      student.Weight = "data from dictionary";
                      student.Height= ""data from dictionary;
                  });
      

      【讨论】:

        猜你喜欢
        • 2015-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        • 1970-01-01
        • 2019-07-28
        相关资源
        最近更新 更多