问题描述:

public class StudentLibrary

{

public int ID { get; set; }

public string Name { get; set; }

public int Status { get; set; }

}

List contails student library

List<StudentLibrary> StudentInfo;

Can you help me extracting student status from list. Hope I make sense.

Thank you in advance

网友答案:

If you have a particular student in mind, and you know their ID:

var status = StudentInfo.Where(x => x.ID == studentId)
                        .Select(x => x.Status)
                        .FirstOrDefault();
网友答案:

Status is integer, but it does not matter - for extracting you can use LINQ projection operator Enumerable.Select:

var statuses = StudentInfo.Select(s => s.Status);

If you want to concatenate all statuses in one string, then String.Join can help you:

var result = String.Join(",",  StudentInfo.Select(s => s.Status));

UPDATE: If you want to select student with max status:

var student = StudentInfo.OrderByDescending(s => s.Status).FirstOrDefault();
网友答案:

Two steps:

  • find the student you want
  • access the member you want

For example:

var status = list[4].Status;
//...

Or:

var student = list.Find(x => x.ID == id);
if(student != null) {
    var status = student.Status;
    //...
}

Note that the latter would be better served by a Dictionary<int,Student>; then you can do:

StudentLibrary student;
if(lookup.TryGetValue(id, out student)) {
    var status = student.Status;
    //...
}
网友答案:
Var status = StudentInfo.Select(x=>x.Status)
相关阅读:
Top