Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know the number of elements of a Iqueryable

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.

like image 575
Jonathan Avatar asked Mar 01 '23 04:03

Jonathan


2 Answers

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();

like image 124
bruno conde Avatar answered Mar 11 '23 05:03

bruno conde


lTax.Count() should do it...

like image 35
Paddy Avatar answered Mar 11 '23 06:03

Paddy