不能将lambda表达式用作动态分派操作的参数 [英] Cannot use a lambda expression as an argument to a dynamically dispatched operation

查看:296
本文介绍了不能将lambda表达式用作动态分派操作的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作

I'm trying to perform the following

int myObject = getValues(someVar).Sum(x => Int32.Parse(x.Price))

该函数看起来如下:

List<dynamic> getValues(string something) {...}

这是我收到的错误: 不能将lambda表达式用作动态调度的操作的参数"

This is the error I'm receiving: "Cannot use a lambda expression as an argument to a dynamically dispatched operation"

如何在类似于LINQ SUM的链式调用中求和List对象的值?

How can I SUM the values of a List object in a chained call similar to LINQ SUM?

推荐答案

您的代码有效.您遇到的问题不在您发布的代码中.此代码将运行.

Your code works. The problem you are having isn't in the code you posted. This code runs.

void Main() {
    int myObject = getValues("12").Sum(x => Int32.Parse(x.Price));
    Console.WriteLine (myObject);
}

List<dynamic> getValues(string something) {
    var item = new { Price = something };
    IEnumerable<dynamic> items = Enumerable.Repeat<dynamic>(item, 2);
    return items.ToList();
}

这将产生输出24.问题可能与类型推断有关,但这只是一个猜测.您应该包括足够的代码以重现该错误,以获得更可靠的答案.

This produces the output 24. The problem may be related to type inference, but that's just a guess. You should include enough code to reproduce the error for a more reliable answer.

这篇关于不能将lambda表达式用作动态分派操作的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆