Dynamically Sorting with LINQ

后端 未结 9 2042
自闭症患者
自闭症患者 2020-12-16 04:57

I have a collection of CLR objects. The class definition for the object has three properties: FirstName, LastName, BirthDate.

I have a string that reflects the name

9条回答
  •  佛祖请我去吃肉
    2020-12-16 05:45

    You can actually use your original line of code

    var results = myCollection.OrderBy(sortProperty);
    

    simply by using the System.Linq.Dynamic library.

    If you get a compiler error (something like cannot convert from or does not contain a definition...) you may have to do it like this:

    var results = myCollection.AsQueryable().OrderBy(sortProperty);
    

    No need for any expression trees or data binding.

提交回复
热议问题