views:

550

answers:

3

I am building an ObjectQuery like this:

        string query = "select value obj from Entities.Class as obj " +
                       "where obj.Property = @Value";

        ObjectQuery<Class> oQuery = new ObjectQuery<Class>(query, EntityContext.Instance);
        oQuery.Parameters.Add(new ObjectParameter("Value", someVariable));

I can now assign this object as a DataSource for a control, or iterate with a foreach loop or even force a materialization to a List, however, I can I count the number of objects that will be returned, without forcing a materialization?

Do I need to create a companion query that will execute a count() or is there a function that will do that for me somewhere?

Thank you.

+2  A: 

ObjectQuery<T> implements IQueryable<T>, so can't you simply use the extension method:

int count = oQuery.Count();

What happens if you execute this? I would have expected the overall query to just do a Count()... (not that I've done much EF...).

Marc Gravell
+1  A: 

The ObjectQuery<T> class implements the IEnumerable<T> interface, which supports the Count() method.

What do you get from this?

int count = oQuery.Count();
Jon Grant
Note that Enumerable.Count() *will* force the data to load; you'd need Queryable.Count() to get the composable behaviour.
Marc Gravell
A: 

if the count<1 then it has found a new record if not it already has that record.

kccer