Print Page | Close Window

Silverlight: Using the Any() method

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2579
Printed Date: 26-Mar-2025 at 5:43am


Topic: Silverlight: Using the Any() method
Posted By: jipock
Subject: Silverlight: Using the Any() method
Date Posted: 22-Mar-2011 at 9:33am
Good Afternoon,
 
I'm wondering whether or not IdeaBlade supports (with the latest version) the Any() method in Silverlight, and if so, what's wrong with this statement. What I'm trying to do is to return a list of objects (Classes) that that a Section that begins on a certain day.
Some of the documentation with DevForce says that .Any wasn't supported because it returns a scalar value, but this really is part of a sub-query.
 
Query:
 

var query = objEntityManager.Classes

                .Include("Sections")

                .OrderBy(op => op.ClassName)

                .Where(op => (this.IsFilterByBeginDate != true || false ||op.Sections.Any((cs,i) => (cs.BeginClassDateTime.Date == DateTime.Now.Date))));

 
The Error returned is:
LINQ to Entities does not recognize the method "Boolean Any[Sections]..... method cannot be translated into a store expression."
 
 
Any thoughts?

 




Replies:
Posted By: jipock
Date Posted: 22-Mar-2011 at 9:44am
Quick Note - the " || false " in the where clause is in there by accident. (It was left over scaffolding from debugging).
 
It should read:
 
.Where(op => (this.IsFilterByBeginDate != true || op.Sections.Any((cs,i) => (cs.BeginClassDateTime.Date == DateTime.Now.Date))));
 
Sorry about confusion.


Posted By: sbelini
Date Posted: 23-Mar-2011 at 10:32am

Hi Jipock,

 

Unfortunately, the Entity Framework does not support Any(Func<T, int, bool>) so you can only execute it against the cache (in either in SL and/or WPF).  

However, your query doesn't really need Any(Func<T, int, bool>), and you could simply use Any(Func<T, bool>) as it is supported by EF and you can run against the query.

So, you query slightly modified:

 

var query = objEntityManager.Classes

                .Include("Sections")

                .OrderBy(op => op.ClassName)

                .Where(op => (this.IsFilterByBeginDate != true || op.Sections.Any(cs => cs.BeginClassDateTime.Date == DateTime.Now.Date)));

 

would execute properly.

We will add a note in our documentation explaining this.

 

Regards,

    Silvio.

 



Posted By: jipock
Date Posted: 29-Mar-2011 at 12:47pm
I just tried the code you mentioned, however, the code does not compile. It says there was no method of .Any that accepts one argument.
 
Is this an Extension method in another DLL?
 
 


Posted By: sbelini
Date Posted: 29-Mar-2011 at 3:08pm
You will only need System.Linq and IdeaBlade.EntityModel.
 
I have attached is a simple Silverlight sample against NorthwindIB running a similar query as provided before.
 
http://www.ideablade.com/forum/uploads/892/Any.zip - uploads/892/Any.zip
 
Silvio.


Posted By: jipock
Date Posted: 05-Apr-2011 at 5:30am
Got it -
 
I was simply missing "using System.Linq;" as a declaration....
 
Gotta love overloaded functions in different namespaces..



Print Page | Close Window