Reputation: 12025
I'm starting with linqToSql and I still have some problems making my queries.
For example, I want to retrieve in the same query two values, a subset of elements, and an integer with the count of the elements. This is how I did it separately:
int totalElements;
IEnumerable<ClsTax> result;
result = (from t in db.tax
select new ClsTax { Tax = t.tax1, Increase = t.increase }).Take(25);
totalElements = (from t in db.tax
select new ClsTax { Tax = t.tax1, Increase = t.increase }).Count();
As you can see, I can't use result.count() because I used Take(25) to obtain only the firsts 25 elements, and I want to know how many elements are in the database.
Are there any way to obtain both values using only one linq query?
Thanks!
Upvotes: 0
Views: 183
Reputation:
How about:
var all = (from t in db.tax select new ClsTax { Tax = t.tax1, Increase = t.increase });
var count = all.Count();
var result = all.Take(25);
bit simpler?
Upvotes: 3
Reputation: 2467
Try this:
var result = (from t in db.tax group t by t.ID into g select new { Tax = g.First().tax1, Increate = t.First().Increase, TotalElements = g.Count() });
Upvotes: 1