依靠 IEnumerable
我正在使用 Rob Conery 的 Massive ORM。
有没有一种优雅的方法来对返回的记录集进行计数?
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++;
}
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++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用扩展方法成员语法来调用它,而是尝试直接调用静态方法。
Rather than calling it using the extension method member syntax, try calling the static method directly.
你可以采纳sehe的答案,就是投射结果。
相反,请了解您从 Query 成员函数中获得的内容。事实上,您正在获得一个动态类型的 IEnumerable,而 var 对此有麻烦。
将此行更改
为此
计数将显示,而无需进行任何转换。
You can take sehe's answer, which is to cast the result.
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
To this
Count will show up without having to do any casting.
问题问得有点过了。您实际上并没有对
IEnumerable
进行计数。您正在尝试对动态
进行计数(希望保存一个IEnumerable
)。最简单的方法是使用强制转换:
The question is a bit off. You're not actually doing a count of an
IEnumerable<dynamic>
. You're trying a count on adynamic
(which hopefully holds anIEnumerable
).The straightforward way to do this is by using a cast: