[ACCEPTED]-Is there a way of using orderby in a forloop C#?-sql-order-by
Accepted answer
Try this:
List<Item> myItems = new List<Item>();
//load myitems
foreach(Item i in myItems.OrderBy(t=>t.name))
{
//Whatever
}
0
new string[] { "d", "c", "b", "a" }
.OrderBy(s => s)
.ToList()
.ForEach(s => MessageBox.Show(s));
0
You don't need a Loop at all. Just use 1 LINQ:
List<MyClass> aList = new List<MyClass>();
// add data to aList
aList.OrderBy(x=>x.MyStringProperty);
foreach
needs an IEnumerable<T>
LINQ order-by takes in one IEnumerable<T>
and 2 gives you a sorted IEnumerable<T>
. So yes, it should 1 work.
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.