views:

143

answers:

3

I have this Linq Query

public IQueryable listAll()
{   
    ModelQMDataContext db = new ModelQMDataContext(); 
    IQueryable lTax = from t
                      in db.tax
                      select new {Tax = t.tax1, Increase = t.increase};
    return  lTax;
}

how can I know the number of elements of lTax?

Thanks.

+4  A: 

Do you really need to return IQueryable? Returning IQueryable from your method doesn't provide much functionality since you can't access the members of an anonymous type without reflection. In this case I suggest that you create a concrete type in the query to be able to return IQueryable<T>:

class TaxIncrease
{
    public int Tax { get; set; }
    public int Increase { get; set; }
}

public IQueryable<TaxIncrease> listAll() {
    ModelQMDataContext db = new ModelQMDataContext();
    return from t in db.tax
           select new TaxIncrease {Tax = t.tax1, Increase = t.increase};
}

and then you can do:

listAll().Count();

bruno conde
This was my first option, but lTax has not a Count method and I don't know Why...
Jonathan
It should do (as an extension method on `IQueryable<T>`). What happens if you try to call `Count()` on it?
Jon Skeet
Ok. I see what your problem is. You should be using the generic version of IQueryable<T> instead and then you can get the *free* extension methods like Count.
bruno conde
+1  A: 

lTax.Count() should do it...

Paddy
bruno was first... ^^
Arnis L.
This was my first option, but lTax has not a Count method and I don't know Why...
Jonathan
A: 

first declare lTax as List, so can have .Count()

Then you can change this to Queryable if you want by lTax.AsQueryable()

dove