LinqBindingList and ThenBy

LinqBindingList and ThenBy

Old forum URL: forums.lhotka.net/forums/t/7236.aspx


Woggly posted on Tuesday, July 07, 2009

Hi!

I am trying to do some sorting and filtering with the help of the LinqBindingList. This worked really fine until my boss noticed, that the new sorting (my sorting) does not exactly match the old way of sorting.
This is what I am doing:
LinqBindingList temp = this.EntitySetModel.Where(ex) as LinqBindingList;
LinqBindingList sortedTempList = (temp.OrderBy(item.DBName) as LinqBindingList);
I am using DynamicLinq to pass in the Sortparameter when the user clicks on a column in a grid.
Now the problem:
The Transactions shown in the grid can have the same date in property Valuta. When you sort the grid ascending with this property the transactions do not have the right order. It seems as if the bindinglist uses the ID of the transaction as second parameter. This is ok, but it should sort the matching date transaction not ascending but descending.
So I tried to do this:
LinqBindingList temp = this.EntitySetModel.Where(ex) as LinqBindingList;
LinqBindingList sortedTempList = (temp.OrderBy(item.DBName) as LinqBindingList).ThenByDescending(b => b.BuchungID) as LinqBindingList;
Although I am using ThenBy the transactions are sorted the same as before.

Does anybaody have an idea how I can sort by date and then by Id in another direction? Or can anybody see if I am doing something wrong?

JoeFallon1 replied on Tuesday, July 07, 2009

It looks like your code is operating against 2 Lists. That could be an issue.

Try combining your linq code into a single statement.

myList.OrderBy("something).ThenByDescending("somethingelse")

Joe

 

Woggly replied on Wednesday, July 08, 2009

I changed the code to this:
LinqBindingList temp = (this.EntitySetModel.Where(ex).OrderBy(item.DBName) as LinqBindingList).ThenByDescending(b => b.BuchungID) as LinqBindingList;

But it does not change anything it is still not sorted like I expect it to be. Is the Lambda-Expressen in ThenBy b=> b.BuchungID ok?

JoeFallon1 replied on Wednesday, July 08, 2009

That is still 2 different lists!

See where you are casting to LBL? That is one list. Then you are calling ThenBy on it which is the 2nd list.

It needs to be a single statement as I showed above.

Try it out using hard coded values to prove that it will work and then figure out the lamdas.

Joe

 

Woggly replied on Wednesday, July 08, 2009

But When I am trying to do it without casting to LBL the compiler tells me, that ThenBy is not available!

JoeFallon1 replied on Wednesday, July 08, 2009

Dim sortedList As Csla.LinqBindingList(Of SomeBO)

Dim query As System.Linq.IOrderedQueryable(Of SomeBO) = From x In mSomeCollection Order By x.Column1 Ascending, x.Column2 Descending

sortedList = CType(query, Csla.LinqBindingList(Of SomeBO))

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

Try code like that shown above.
Joe

Woggly replied on Thursday, July 09, 2009

Thanks, this code sample helped me.
My List gets now sorted like I expected it.

Copyright (c) Marimer LLC