Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq Query OrderBy Multiple

I have a List<Employees> collection that I am trying to select Active employees on.

I must be doing something wrong, because I see numerous examples on here showing that I can do this, yet Visual Studio is telling me I can not because:

Cannot implicitly convert type 'System.Linq.IOrderedEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

What is wrong with this picture? LastName and FirstName (as you might suspect) are both String values, and Active is a Boolean.

range variable

DataGridView1.DataSource = null;
List<AcpEmployee> emps = from e in employeeInfo.Employees
                         where e.Active 
                         orderby e.LastName, e.FirstName descending
                         select e;

1 Answers

You should use ToList at the end of your query:

List<AcpEmployee> emps = (  from e in employeeInfo.Employees
                            where e.Active
                            orderby e.Active orderby e.LastName, e.FirstName descending
                            select e).ToList();

or using the methods directly:

List<AcpEmployee> emps = employeeInfo.Employees
    .Where(e => e.Active)
    .OrderBy(e => e.LastName)
    .ThenByDescending(e => e.FirstName)
    .ToList();
like image 138
Lee Avatar answered Jul 31 '26 04:07

Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!