-2

In below code why filteredStudentNames holds only 2 elements. As per my understanding, it is reference type so when I update the studentList object with one more item that results should also be part of filteredStudentNames.

In the case of studentNames object, I am getting the updated record also. So why not in the case of filteredStudentnames?

static void Main(string[] args)
    {
        IList<Student> studentList = new List<Student>() {
                    new Student() { StudentID = 1, StudentName = "John", age = 18 } ,
                    new Student() { StudentID = 2, StudentName = "Steve",  age = 21 } ,
                    new Student() { StudentID = 3, StudentName = "Bill",  age = 18 } ,
                    new Student() { StudentID = 4, StudentName = "Ram" , age = 20  } ,
                    new Student() { StudentID = 5, StudentName = "Ron" , age = 21 }
                };

        var filteredStudentNames = from s in studentList 
                                   where s.age > 20
                                   select s;

        var studentNames = from s in studentList
                           select s;

        IList<Student> stud = studentList;
        studentList.Add(new Student { StudentID = 27, StudentName = "Test" });
Hary
  • 5,690
  • 7
  • 42
  • 79
Saurabh
  • 35
  • 6

2 Answers2

1

filteredStudentNames is queried with condition age > 20. On adding new Student you have not initiated the age property which means by default it is 0.

So it is not reflecting in filteredStudentNames

Try initiating the age property and then query the Count

studentList.Add(new Student { StudentID = 6, StudentName = "Test", age = 21 });

Count:

Console.WriteLine("filteredStudentNames: " + filteredStudentNames.Count()); //Output: 3
Console.WriteLine("studentNames: " + studentNames.Count());   //Output: 6
Hary
  • 5,690
  • 7
  • 42
  • 79
-1

I'm pretty sure this will be work for you.

static void Main(string[] args)
{
    IList<Student> studentList = new List<Student>() {
                new Student() { StudentID = 1, StudentName = "John", age = 18 } ,
                new Student() { StudentID = 2, StudentName = "Steve",  age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill",  age = 18 } ,
                new Student() { StudentID = 4, StudentName = "Ram" , age = 20  } ,
                new Student() { StudentID = 5, StudentName = "Ron" , age = 21 }
            };

    var filteredStudentNames = from s in studentList where s.age > 20
                       select s;

    var studentNames = from s in studentList
                       select s;

    IList<Student> stud = studentList;
    studentList.Add(new Student { StudentID = 27, StudentName = "Test", age = 25 });


    var filteredStudentNames = from s in studentList where s.age > 20
                       select s;

}

You have to execute link after the adding by new entry and you have to add age for the new entry. Thanks.

Thili77
  • 1,061
  • 12
  • 20
  • 1
    `You have to execute link after the adding by new entry.` No you don't. https://stackoverflow.com/questions/7324033/what-are-the-benefits-of-a-deferred-execution-in-linq Also, your code does not compile. – mjwills Oct 31 '18 at 11:02