views:

297

answers:

1

I have an EntityDataSource with OnSelected event (fired after finished query). The event handler has event args of type EntityDataSourceSelectedEventArgs e. Query runs fine without error and the IEnumerable e.Results contains 1 object (I can run through a non-empty foreach-loop) but e.TotalRowCount returns -1.

Does somebody have an idea what this means and what information TotalRowCount actually expresses? How can I determine the number of selected objects if not by using TotalRowCount?

+3  A: 

From http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.selected(VS.100).aspx:

    The TotalRowCount property of the EntityDataSourceSelectedEventArgs 
object shows the total number of objects in all pages, regardless of the 
values passed by the data-bound control for paging. 

    TotalRowCount is only retrieved if the data-bound 
control needs it, such as if paging is enabled.

Is your data-bound control using paging?

DaveB
No, it doesn't use paging. OK, thanks, that explains why TotalRowCount is useless without paging. But how can I determine the number of objects then?
Slauma
Since e.Results is an IEnumerable, how about e.Results.Count() ?
DaveB
e.Results is an `System.Collections.IEnumerable` which does not seem to have a `Count()` method or property - in contrast to `System.Collections.Generic.IEnumerable<T>`.
Slauma
Try importing the Linq namespace and using it's extension method.
DaveB
System.Linq and System.Data.Linq was already imported but it doesn't help. The extension method seems only to be available for the generic version of IEnumerable but not for the non-generic version. After looking at various other discussions here about IEnumerable I think the only way is really to iterate through IEnumerable and to count the objects.
Slauma
This post may be of some help if you want to write your own extension method. http://stackoverflow.com/questions/1155619/ienumberable-to-something-that-i-can-get-a-count-from
DaveB