Legion
Legion

Reputation: 3447

Getting Count() for basic IQueryable instance

I need to determine at runtime which view to populate recordSet from. However when I declare recordSet as IQueryable I lose access to functions like Count().
If I declare recordSet as IQueryable<View_A> then I have access, but I've locked in the type. Is there some more generic type I could use with IQueryable?

IQueryable recordSet;

if (Flag)
    recordSet = db.View_A.Where(v => v.ID == Id);
else
    recordSet = db.View_B.Where(v => v.ID == Id);

int recordCount = recordSet.Count(); //complains about Count();

Upvotes: 2

Views: 100

Answers (1)

Crono
Crono

Reputation: 10478

The Count extension is meant for IQueryable<> type, which is why you cannot use it on IQueryable.

However, covariance should allow your code to work if you declare your recordset this way:

IQueryable<Object> recordSet;

As noted by @hvd in the comments below, covariance here would only work with reference types.

Upvotes: 1

Related Questions