Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using DataSourceLoader with custom sort #388

Closed
MihaMarkic opened this issue Sep 30, 2019 · 5 comments
Closed

Using DataSourceLoader with custom sort #388

MihaMarkic opened this issue Sep 30, 2019 · 5 comments
Labels

Comments

@MihaMarkic
Copy link

MihaMarkic commented Sep 30, 2019

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?

@AlekseyMartynov
Copy link
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:

@MihaMarkic
Copy link
Author

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

@AlekseyMartynov
Copy link
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.

@MihaMarkic
Copy link
Author

MihaMarkic commented Oct 18, 2019

@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.

@AlekseyMartynov
Copy link
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
Labels
Projects
None yet
Development

No branches or pull requests

2 participants