How TO Add Data TO List Of Object C# [Resolved]

Posted by Kasani007 under LINQ on 8/3/2016 | Points: 10 | Views : 338 | Status : [Member] | Replies : 3
How TO Add Data TO List Of Single Object


public class Student
{
public int StudentId{get;set;}
public string StudentName{get;set;}
public Address Address{get;set;}
}

public class Address
{
public int AddressId{get;set;}
public string DoorNo{get;set;}
public string StreetName{get;set;}
public string City{get;set;}
public string Country{get;set;}
}

Now, I Have List<Student>

List<Student> student=new List<Student>();
student.AddRange(student1);
// As student1 is also a List

Then How To Add Address along with Add method

student.AddRange(student1.Address);    

//As student1 is also a list How can we add List Without using foreach To exact List Object




Responses

Posted by: Rajnilari2015 on: 8/3/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Kasani007 Sir,
Here is how to do so

List<Student> student=new List<Student>(); 

Address objAddress = new Address(){AddressId =1,DoorNo = "#1234",StreetName = "Some Street", City = "Some City", Country="Some Country"};

Student student1 = new Student(){StudentId = 11, StudentName = "Some Student Name", Address = objAddress};

student.Add(student1);


Hope that helps

--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Joginder on: 8/3/2016 [Member] Starter | Points: 25

Up
0
Down
Have a look.
public class Student

{
public int StudentId { get; set; }
public string StudentName { get; set; }
public Address Address { get; set; }
}

public class Address
{
public int AddressId { get; set; }
public string DoorNo { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}

List<Student> student = new List<Student>();
Address address = new Address();
address.City = "Kaithal";
student.Add(new Student
{
StudentId = 10,
StudentName = "Singh",
Address=address
});


Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhuvanesh6 on: 8/4/2016 [Member] Starter | Points: 25

Up
0
Down
Sample:

List<int> primes = new List<int>();
primes.Add(2);
primes.Add(3);
primes.Add(5);
primes.Add(7);


Your Code:
Use Add() instead of AddRange() method.


Bhuvan

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response