Skip to content
Advertisement

LinqToSql OrderBy has no Effect

I am using a LinqToSql-DataSource for a GridView in this way:

Now i want to order the Selection. At first the “stammVerein”-Column of Table “mitgliedschaft” descending AND the Column “eintritt” of Table “mitgliedschaft”. I have tried several ways:

AND:

AND:

But nothing of this has any Effects ! I am very new in this kind of DataSource and Linq.

Can anyone help me achieving this order ?

Advertisement

Answer

Items within a grouped result will not retain their order. Depending on how you want to factor in the ordering, you will need to do it after the group by, and before, and/or after your First

To accomplish this, it will be easiest if you map the relationships in EF with navigation properties rather than substituting SQL with Linq QL (joins and such)

Using the following base query:

For instance, after the group by, you will have sets of records grouped by Person. If you want the first Person with an earliest related record:

This is taking a wild guess at your schema & entity relationships, but hopefully it will help you work out something that fits. I can only guess at what eintritt is and how it relates to your entity model.

The initial query takes just your base entities that you want to group, and groups them by the related entity. The result of that grouping will be a set of Grouped mitgliedschafts with a key being the Person. To Order those groups by the person with the most recent mitgliedschafts we use an orderby on the Key’s associated mitgliedschafts using the Max value for the collection given a descending order request.

The First then gives us the first grouped collection of mitgliedschafts.

Then if you want to sort the resulting list of mitgliedschafts after getting the person with the most recent one:

The 2nd set of OrderBy clauses apply to the selected group, or the mitgliedschafts.

To compose the desired view model, Insert a Select() to build the view model from the mitgliedschafts before the ToList().

With the navigation properties this can probably be done without resorting to a group by. On a hunch, something like this should return something similar:

Anyhow, hopefully that gives you some ideas on things to try if you have the navigation properties mapped or can set those up.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement