HowTo use LinqToCsla

HowTo use LinqToCsla

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


Luzius posted on Monday, March 30, 2009

Hi,

for the last couple of hours I've been trying to use LinqToCsla and I'm somehow lost:

I have a BO TaskEdit which contains an editable List of other BOs. Now I try to sort the list by a simple DateTime property:

private static readonly PropertyInfo<UrgencyEditCollection> urgenciesProperty = RegisterProperty<UrgencyEditCollection>(c => c.Urgencies);

public LinqBindingList<UrgencyEdit> Urgencies
{
   
get
   
{
      return from urgency in GetProperty(urgenciesProperty)
                  orderby urgency.StartDate
                  select urgency;
   }
}

VS2008 makes a red line below the Linq Query and the message is:

Cannot convert expression type 'System.Linq.IOrderedQueryable<Tasks.Library.UrgencyEdit>' to return type 'Csla.LinqBindingList<Tasks.Library.UrgencyEdit>'

Both TaskEdit and UrgencyEdit derive indirectly via 2 additional base classes from BusinessBase. UrgencyEditCollection derives directly from BusinessListBase.

Thanks for any hints.
Luzius

RockfordLhotka replied on Monday, March 30, 2009

Are you using the 3.6.2 release candidate?

I know there were some issues with orderby not returning a LinqBindingList that have (hopefully) been addressed in 3.6.2.

JoeFallon1 replied on Monday, March 30, 2009

Your query should be defined as IOrderedQueryable.

Then you should be able to cast it to LinqBindingList.

Something like this:

Dim query As IOrderedQueryable =
from urgency in ...
orderby ..

Return CType(query, LinqBindingList(Of UrgencyEdit)

Joe

Luzius replied on Tuesday, March 31, 2009

Thanks,

I somehow thought that no explicit cast was necessary - my fault.

This way it works very well, even with more than one orderby criteria. (I think this was the issue with the 3.6.2 beta).

I still have an issue with binding this list to a WPF ListBox. When I bound the original BusinessListBase Collection to the Listbox, all updates to the list were automatically displayed. Now after binding the LinqBindingList, I have to call Rebind() in the CslaDataProvider. Is this necessary or am I doing something wrong?

Luzius

JoeFallon1 replied on Tuesday, March 31, 2009

Luzius:

Thanks,



This way it works very well, even with more than one orderby criteria. (I think this was the issue with the 3.6.2 beta).





I think standard LINQ queries work with any number of OrderBy criteria in 3.6.2. It is the Dynamic Linq queries that needed fixing for 3 or more columns sorts. The key issues is that std LINQ calls deep into the code differently than dynamic LINQ. Std LINQ passes each part of the orderby expression as separate calls. Dynamic LINQ passes the whole thing in one shot. The fix for 3.6.3 now takes that into account.

Joe

Copyright (c) Marimer LLC