none
Linq to Entities RRS feed

  • Question

  • Hi All,

    I'd like to extend Orderby so I can dynamically sort on fields that are determined at runtime. I've already extended it once, however this does not solve an additional need that has come up.

    I'm joining two entities and i'd like to orderby a field or fields that can be from either entity. In the following LINQ, i'm sorting on two fields from both the outer and inner entity: (l is a IQueryable previously declared)

     l = l.Join(context.vw_ItemCitation, x => x.ItemInstance.Item_ID, y => y.Item_ID,
                                                 (x, y) => new
                                                          {
                                                              x = x,
                                                              y = y
                                                          }
                                               ).OrderBy(z => z.y.Title) 
                                                .ThenBy(z => z.x.ItemInstance.BatteryStatus.StatusName)
                                                .Select(z => z.x);

    So, instead of Title and StatusName i'd like to be able to dynamically substitute field names in.

    Any help appreciated.

     

     

     

    Monday, January 2, 2012 9:19 PM

Answers

  • Hi NormalDistribution,

    Welcome to MSDN Forum.

    Based on the issue, if you want to create a Linq to Entities statement dynamically, you can use the linq dynamic library. Here's a link you can refer to - Dynamic Linq. There's library download site and sampe download site in it. With the dynamic library, you can create your Linq to Entities through string concatenating.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, January 4, 2012 2:16 AM
    Moderator