ExpressionObserver and Where

Sep 27, 2010 at 11:59 AM

Hi,

I use Obtics with Silverlight 4. I wonder about my experience.

In the following example _versionableObjectsViewModels is observable, and I would like to filtered collection with this predicate: item.LastVersion! = null.

My first try was:

var xx = _versionableObjectsViewModels.Where(vovm => vovm.LastVersion != null);

I realized that if an item changes LastVersion value with null, xx does not changed... I therefore looked for a long time to find:

var xx = ExpressionObserver.Execute(() => _versionableObjectsViewModels.Where(vovm => ExpressionObserver.Execute(() => vovm.LastVersion != null))).Cascade();

Is this the right way? If so why don't have not do simpler?

Best regards,

Vincent

Coordinator
Sep 28, 2010 at 11:33 AM

Hi Vincent,

I think something like:

var xx = ExpressionObserver.Execute(() => _versionableObjectsViewModels.Where(vovm => vovm.LastVersion != null)).Cascade();

should work better.

Regs,

Thomas

 

 

Sep 28, 2010 at 2:00 PM

Hello,

I already tried that, but it does not work: (

Vincent

Coordinator
Sep 28, 2010 at 6:24 PM

Hi Vincent,

The reason your code works, I guess, is because you included the Obtics.Collections namespace in the using directives. The 'Where' method that is being used in that case is the Obtics.Collections.ObservableEnumerable.Where method instead of the System.Linq.Enumerable.Where method.

When working with the ExpressionObserver you should make sure that the System.Linq.Enumerable methods are used. That means including System.Linq instead of Obtics.Collections in your using directives.

ExpressionObserver will translate the call to the System.Linq.Enumberable.Where method to a call to the Obtics.Collections.ObservableEnumerable.Where method and translate your lambda expression as well. It actually can't do anything with the Obtics.Collections.ObservableEnumerable.Where method itself.

Regs,

Thomas.

Sep 29, 2010 at 1:40 PM

Okay, thanks you, it works, but then in which case the use of Where Obtics.Collections is required ?

Vincent.

Coordinator
Sep 30, 2010 at 8:57 AM

In simple cases I don't think the using of Obtics.Collections would ever be required. You could use it if you would want to construct observable expressions manually. (Not using ExpressionObserver)

ExpressionObserver translates ordinary expressions to observable ones and maps object LINQ methods to observable equivalents in Obtics.Collections.ObservableEnumberable.

Regs,

Thomas

Sep 30, 2010 at 9:53 AM

ok, thanks you :)

Vincent