I'm using Rob Conery's Massive ORM.
Is there an elegant way to do a count on the record set returned?
dynamic viewModelExpando = result.ViewData.Model;
var queryFromMassiveDynamic = viewModelExpando.TenTricksNewestFirst;
//fails as have actually got TryInvokeMember on it
var z = queryFromMassiveDynamic.Count();
//works
int i = 0;
foreach (var item in queryFromMassiveDynamic) {
i++;
}
Rather than calling it using the extension method member syntax, try calling the static method directly.
int count = Enumerable.Count(queryFromMassiveDynamic);
The question is a bit off. You're not actually doing a count of an IEnumerable<dynamic>
. You're trying a count on a dynamic
(which hopefully holds an IEnumerable
).
The straightforward way to do this is by using a cast:
var z = (queryFromMassiveDynamic as IEnumerable<dynamic>).Count();
You can take sehe's answer, which is to cast the result.
var z = (queryFromMassiveDynamic as IEnumerable<dynamic>).Count();
Instead, realize what you are getting from the Query member function. You are in fact getting an IEnumerable of type dynamic and var has trouble with those.
Change this line
var queryFromMassiveDynamic = viewModelExpando.TenTricksNewestFirst;
To this
IEnumerable<dynamic> queryFromMassiveDynamic = viewModelExpando.TenTricksNewestFirst;
Count will show up without having to do any casting.
来源:https://stackoverflow.com/questions/7733305/count-on-an-ienumerabledynamic