I am trying to sort a List like this:
public void Example() { string target = 'hello'; List<string> myStings = new List<string>(); myStings.Add('babab'); myStings.Add('Helll'); myStings.Add('atest'); myStings.OrderBy(each => Distance(each, target)); } public int Distance(string stringA, string stringB) { // Whatever }
The problem is that the list doesn’t get ordered and the Distance method doesn’t get fired (I put a breakpoint in there but doesn’t get hit).
Any help appreciated!
This is because of Linq’s Deferred Execution
So, to see your method working, apply the
ToList()method to yourIOrderedEnumerableso that you will actually be requesting the data, and thus the execution takes place.