user1400915
user1400915

Reputation: 1943

Replacing an object in list with a list in C#

I have an object as shown below:

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;}
}

I also have a dict of Type <key,CustomObject> where the CustomObject contains all of the "Students" class fields plus additional fields.

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;}
}

I have got the dict built in such a way that key is the StudentName and Value is the CustomObject like

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

I have a School object where the Students list

List<Students> studentList = new List<Students>();

just has "StudentName" in the inner Students Object and other fields are empty. I have to find this StudentName in the Dictionary and build the "Students" Object from the "CustomObject" and then attach it to "ClassRoom" . So finally I should be able to get all the fields filled for the Student object(2 Student objects from my example above ,one with HairColor as black and one with Haircolor as brown) when I expand the School object in the immediate window and navigate to "School",data coming from dictionary.

. In the example, One student having 2 PropertyObjects is just hypothetical only.

How would I continue getting the result. Is it something like :

studentList.Select(x => x.classAttribute)
           .Select(y => y.students)
           .Where(z=>z.StudentName == dict.key);

But the problem is I have to build Student Object from CustomObject before attaching it also. So, how I do it with linq?

Upvotes: 0

Views: 101

Answers (2)

Kaushal
Kaushal

Reputation: 1359

This may help

       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;
            });

Upvotes: 0

Ciprian Lipan
Ciprian Lipan

Reputation: 340

I believe this is what you are looking for:

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();

Upvotes: 3

Related Questions