none
LINQ to Entity orderby

    Question

  • I need to orderby multiple fields (up to 3 fields out of 63) on an object. However these fields are a user specifc configuration and is unknown until runtime so basically I need to be able to dynamically set the orderby of up to 3 fields asc OR desc at runtime.

    Can I do this with LINQ to Object?

    • Moved by Alex Feng (SQL) Wednesday, May 19, 2010 6:13 AM move to an appropriate forum (From:SQL Server Spatial)
    Monday, May 17, 2010 5:02 PM

Answers

  • Hello,

     

    Welcome to EF forum!

     

    From your post, I think you want a dynamic LINQ to Object query.   I would recommend you use the Dynamic LINQ Library.   Although Scott’s blog is for using this library on LINQ to SQL, it is easy to make it work in LINQ to Objects queries.   

     

    Please download the Dynamic Query sample, refer to the output assembly or directly copy the Dynamic.cs file into your project.   After adding the namespace “System.Linq.Dynamic”, we can make a dynamic orderby query like:

    =================================================================================

                List<Person> persons = new List<Person>();

                persons.Add(new Person { ID = 100, Name = "Hello" });

                persons.Add(new Person { ID = 20, Name = "Hello" });

                persons.Add(new Person { ID = 20, Name = "Apple" });

     

                var query = persons.AsQueryable().OrderBy("ID, Name descending").ToList();

    =================================================================================

    Here I order the List of Person by each Person’s ID and Name (descending order) properties.   The query string can be dynamically built based on your request during runtime.   For detailed information about how to use the Dynamic LINQ Library, please see the Dynamic Expressions.html file under the sample folder. 

     

    Of course, we can also create the Expression Trees manually by to solve the problem, http://msdn.microsoft.com/en-us/library/bb882637.aspx.  But I think using the library is much easier.  J  

     

    BTW, the thread title is “LINQ to Entity orderby”, so if you are actually working on Entity Framework, the Dynamic LINQ Library can work great as well.  

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, May 19, 2010 8:06 AM

All replies

  • This is the SQL Server spatial forum - you're more likely to get a response if you post your question on the LINQ forum:

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads/


    Beginning Spatial with SQL Server http://www.apress.com/book/view/1430218290
    Monday, May 17, 2010 8:57 PM
  • Hello,

     

    Welcome to EF forum!

     

    From your post, I think you want a dynamic LINQ to Object query.   I would recommend you use the Dynamic LINQ Library.   Although Scott’s blog is for using this library on LINQ to SQL, it is easy to make it work in LINQ to Objects queries.   

     

    Please download the Dynamic Query sample, refer to the output assembly or directly copy the Dynamic.cs file into your project.   After adding the namespace “System.Linq.Dynamic”, we can make a dynamic orderby query like:

    =================================================================================

                List<Person> persons = new List<Person>();

                persons.Add(new Person { ID = 100, Name = "Hello" });

                persons.Add(new Person { ID = 20, Name = "Hello" });

                persons.Add(new Person { ID = 20, Name = "Apple" });

     

                var query = persons.AsQueryable().OrderBy("ID, Name descending").ToList();

    =================================================================================

    Here I order the List of Person by each Person’s ID and Name (descending order) properties.   The query string can be dynamically built based on your request during runtime.   For detailed information about how to use the Dynamic LINQ Library, please see the Dynamic Expressions.html file under the sample folder. 

     

    Of course, we can also create the Expression Trees manually by to solve the problem, http://msdn.microsoft.com/en-us/library/bb882637.aspx.  But I think using the library is much easier.  J  

     

    BTW, the thread title is “LINQ to Entity orderby”, so if you are actually working on Entity Framework, the Dynamic LINQ Library can work great as well.  

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, May 19, 2010 8:06 AM
  • Hi,

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

     

    If you need further assistance, please feel free to let me know.   I will be more than happy to be of assistance.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, May 21, 2010 5:06 AM