Skip to content

Using DataSourceLoader with custom sort #388

Closed

Description

@MihaMarkic

For various reasons, I want to pass an IQueryable containing an OrderBy statement to DataSourceLoader.Load.

Basicallly something like

var items = db.Items.OrderBy(i => i.Name).Select(i => i.Name);

 // where loadOptions.Sort = new SortingInfo[0];
var result = DataSourceLoader.Load(items, loadOptions);

When working with version 2.4.4 Load did honor the OrderBy, but 2.5.0 ignores it completely (both ignore it actually). So, how I go with custom sorting in this case?

Activity

  1. AlekseyMartynov commented on Oct 1, 2019

    @AlekseyMartynov
    Contributor

    You can use the DataSourceLoadOptionsBase.DefaultSort property or build a custom sort list:

    var mySort = . . .;
    loadOptions.Sort = mySort; 

    The following support ticket may be relevant:

  2. MihaMarkic commented on Oct 2, 2019

    @MihaMarkic
    Author

    @AlekseyMartynov Well, that most probably won't work if I were to use a client side projection.

  3. AlekseyMartynov commented on Oct 2, 2019

    @AlekseyMartynov
    Contributor

    Would you please add a code sample to illustrate what exactly won't work?

    Internally, DataSourceLoader builds a LINQ expression by appending Where, OrderBy, etc to the source query. For example:

    db.Items.OrderBy(i => i.Name).Select(i => i.Name)
        .Where(...)
        .OrderBy(...)

    Subsequent OrderBy cancels previous OrderBy. To keep sorting by Name, you can use DefaultSort or modify Sort as suggested in #388 (comment).

    Alternatively, we can add a boolean option to prevent any sort inside DataSourceLoader when the source query is pre-sorted.

  4. MihaMarkic commented on Oct 18, 2019

    @MihaMarkic
    Author

    @AlekseyMartynov Sorry for the delay

    Imagine a projection to a

    class ClientSide {
      public string LowercaseName { get; set; }
    }
    
    DataSourceLoader.Load(db.Items.Select(i => new ClientSide { LowercaseName = string.ToLower(i.Name) } , loadOptions});

    Perhaps I'm wrong, but Select in this case is client side and applying sort and/or filter on it, would cause a problem because it will have to fetch all the data from the database first.

  5. AlekseyMartynov commented on Oct 18, 2019

    @AlekseyMartynov
    Contributor

    Thank you for the update. Now I better understand the issue. You need to sort original items by Name and then project them so that Name becomes LowercaseName.

    I just posted a comment about DTO mapping. Please review it and let me know if such a feature would address your requirements. In the interim, check if you can use the workaround suggested in #378 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions