依靠 IEnumerable

发布于 2024-12-09 08:26:10 字数 387 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花桑 2024-12-16 08:26:10

不要使用扩展方法成员语法来调用它,而是尝试直接调用静态方法。

int count = Enumerable.Count(queryFromMassiveDynamic);

Rather than calling it using the extension method member syntax, try calling the static method directly.

int count = Enumerable.Count(queryFromMassiveDynamic);
瞎闹 2024-12-16 08:26:10

你可以采纳sehe的答案,就是投射结果。

var z = (queryFromMassiveDynamic as IEnumerable<dynamic>).Count();

相反,请了解您从 Query 成员函数中获得的内容。事实上,您正在获得一个动态类型的 IEnumerable,而 var 对此有麻烦。

将此行更改

var queryFromMassiveDynamic = viewModelExpando.TenTricksNewestFirst;

为此

IEnumerable<dynamic> queryFromMassiveDynamic = viewModelExpando.TenTricksNewestFirst;

计数将显示,而无需进行任何转换。

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.

如果没有 2024-12-16 08:26:10

问题问得有点过了。您实际上并没有对 IEnumerable 进行计数。您正在尝试动态进行计数(希望保存一个IEnumerable)。

最简单的方法是使用强制转换:

 var z = (queryFromMassiveDynamic as IEnumerable<dynamic>).Count();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文