Question

I have a list:

     public List<View_Group> GetGroupID_FromEmpNo(Int16 x)
    {
        List<View_Group> query = (from a in contxt.View_Group
                                  where a.EmpNo == x
                                  select new View_Group 
                                 { 
                                 id = a.id,
                                 EmpNo = a.EmpNo 
                                 }).ToList();
        return query.ToList();
    }

a.EmpNo column have two same values, same Employee numbers (2884). If I can simply retrieve the first value using .FirstOrDefault() to get the first a.id, how do I retrieve the last value of a.id using the same Employee number (2884)?

I'm thinking using foreach loop through an array but this is a list and I am sort of new to looping, arrays, especially lists in C#.

Regards

Was it helpful?

Solution

You can use orderby descending and then FirstOrDefault()

var lastitem = (from a in contxt.View_Group
     where a.EmpNo == x
     orderby a.id descending
     select new View_Group 
     { 
       id = a.id,
       EmpNo = a.EmpNo 
     }).FirstOrDefault();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top