I have a for loop where i want to orderby the name alphabetically
a
b
c
d
looking how to do this, wondered e开发者_如何学Goven if i could use linq orderby inside the forloop?
Try this:
List<Item> myItems = new List<Item>();
//load myitems
foreach(Item i in myItems.OrderBy(t=>t.name))
{
//Whatever
}
new string[] { "d", "c", "b", "a" }
.OrderBy(s => s)
.ToList()
.ForEach(s => MessageBox.Show(s));
You don't need a Loop at all. Just use 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 gives you a sorted IEnumerable<T>
. So yes, it should work.
精彩评论