views:

56

answers:

3

I am new to LINQ. I have a class like this:

public class StudentResults
{

    public int Id { get; set; }

    public ResultsStatus StatusResult { get; set; }

    public string Message { get; set; }

    public StudentDetails Detail { get; set; }

}

There is a method that returns a List of the above class to a variable

I need to iterate thru that variable and put the students into two different classes. PassedStudents, FailedStudents based on ResultsStatus.

Here is what I have tried but it doesnt work

 var studIds = from r in StudList
 group r by r.Id into GroupedData
  select new 
  {
   //what to put here
  };
  foreach(var crs in studIds)
  {
   //what to put here to get all student names from the Detail property.
  }

Is there another way?

+3  A: 

Personally, I would just treat this as two queries:

var passed = StudList.Where(student => student.StatusResult == ResultStatus.Passed);
var failed = StudList.Where(student => student.StatusResult == ResultStatus.Failed);

Console.WriteLine("Passed...");
foreach(var student in passed)
   Console.WriteLine(student.Detail.Name);

Console.WriteLine("Failed...");
foreach(var student in failed)
   Console.WriteLine(student.Detail.Name);
Reed Copsey
Hi Reed : Is there any specific reasons to create two list.
saurabh
@saurabh: The OP wanted to separate the collection based on this property. From a usage standpoint, having it as two IEnumerable<Student> will make using the results easier. That being said, this isn't making two lists -it's streaming results from a single collection into two IEnumerable<T> instances, but they're streamed as used. (I never actually evaluate the enumerables until the foreach...)
Reed Copsey
@Reed : If i can just group student based upon ResultStatus than i can avoid one foreach loop
saurabh
@saurabh: The group is FAR more expensive than the foreach loop, though ;) Plus, you have to do the segregation within the foreach (you'll still iterate over each group's details with a foreach, somehow...)
Reed Copsey
@Reed : thanks for clearing this.
saurabh
+2  A: 

Sounds like you'd like 2 lists: one for failed, and one for passed.

Try this:

List<StudentResults> failed = StudList.Where(x=>x.ResultStatus=="Failed")
                                      .ToList();

List<StudentResults> passed = StudList.Where(x=>x.ResultStatus=="Passed")
                                      .ToList();
p.campbell
@p.campbell : Is there any specific reasons to create two list?
saurabh
@saurabh: two lists is what the OP asked for. "put the students into two different"... Did you read the question?
p.campbell
+1  A: 

Simply use "where". For instance, to get all the "PassedStudents":

var passedStudents = from student in StudentList
                     where student.StatusResult == ResultsStatus.PassedStudent
                     select student;

foreach (var student in passedStudents)
{
    Console.WriteLine("[{0}.] {1} passed.", student.Id, student.Detail.Name);
}

You can also write the query using lambda expressions:

var passedStudents = 
    StudentList.Where(student => student.StatusResult == ResultsStatus.PassedStudent);
Wonko the Sane
Obviously, like the other solutions posted, I would suggest a similar query for the "Failed" students.
Wonko the Sane