x.Item!= "SF" && x.AdSize == minadS...">

LInq Order By By Desc

I use "Linq" to filter the list of objects and sort them, for example

myList.Where(x => x.Item!= "SF" && x.AdSize == minadSize) .OrderBy(x => x.ManufacturingDate) .OrderBy(x=>x.ExpiryDate); 

I doubt that I am doing it right or not, if I want to "sort" by several fields, then it is necessary to use several Order By can not clauses with the help of one "OrderBy"

+6
source share
1 answer

Do not use multiple ThenBy calls - use ThenBy and then ThenBy :

 var query = myList.Where(x => x.Item!= "SF" && x.AdSize == minadSize) .OrderBy(x => x.ManufacturingDate) .ThenBy(x => x.ExpiryDate); // Could add more ThenBy calls 

If you use OrderBy twice, it will change the order of the list already ordered by date by the expiration date, while I assume that you only want to order by the expiration date for goods with an equal manufacturing date, which is what the above does.

Obviously there is also a ThenByDescending method. For instance:

 var query = people.OrderBy(x => x.LastName) .ThenBy(x => x.FirstName) .ThenByDescending(x => x.Age) .ThenBy(x => x.SocialSecurity); 
+14
source

Source: https://habr.com/ru/post/890224/


All Articles