简体   繁体   English

如何调试此错误:“无法将Lambda表达式转换为字符串”?

[英]How can I debug this error: “Cannot Convert Lambda Expression to String”?

I am trying to debug the following code, but it is not working. 我正在尝试调试以下代码,但无法正常工作。

I upload this pic http://i68.tinypic.com/2rqoaqc.jpg 我上传了这张图片http://i68.tinypic.com/2rqoaqc.jpg

This is my code: 这是我的代码:

using System;
using System.Text;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {   
         string input = "code1,code2,#c55+35+97#g,coden,code3,code4,#c44+25+07#gcoden";

         string output = Regex.Replace(
             input, 
             "#c(.*?)#g", 
             m => "#c" + m.Groups[1].Value.Split('+').Sum(int.Parse) + "#");

         Console.WriteLine(output);    
    }
}

And these are the errors I am getting: 这些是我得到的错误:

ERROR 1: 错误1:

'int int.Parse(string)' has the wrong return type (CS0407) - 'int int.Parse(string)'具有错误的返回类型(CS0407)-

ERROR 2: 错误2:

The call is ambiguous between the following methods or properties: 'System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable, System.Func)' and 'System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable, System.Func)' (CS0121) 该调用在以下方法或属性之间是不明确的:'​​System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable,System.Func)'和'System.Linq.Enumerable.Sum(System.Collections.Generic.IEnumerable ,System.Func)'(CS0121)

ERROR 3: - Cannot convert m "lambda" to string 错误3: -无法将m“ lambda”转换为字符串

Cannot convert lambda expression to type 'string' because it is not a delegate type (CS1660) 无法将Lambda表达式转换为“字符串”类型,因为它不是委托类型(CS1660)

You need to use an explicit lambda rather than int.Parse() : 您需要使用显式lambda而不是int.Parse()

    string output = Regex.Replace(
        input,
        "#c(.*?)#g",
        m => "#c" + m.Groups[1].Value.Split('+').Sum(v => int.Parse(v)) + "#");

Notice I replaced int.Parse with v => int.Parse(v) . 注意,我用v => int.Parse(v)替换了int.Parse Sample fiddle . 样品提琴

Interestingly enough, this compiles and works as desired in c# 6.0: 有趣的是,它可以按照c#6.0中的要求进行编译和工作:

    string output = Regex.Replace(
        input,
        "#c(.*?)#g",
        m => "#c" + m.Groups[1].Value.Split('+').Sum(int.Parse) + "#");

Sample Roslyn fiddle . 样品罗斯林小提琴 I'm not sure where this change is documented, maybe under New Language Features in C# 6: Improved overload resolution : 我不确定在哪里记录了此更改,也许在C#6的新语言功能:改进的重载分辨率下

There are a number of small improvements to overload resolution, which will likely result in more things just working the way you'd expect them to. 对于重载分辨率有许多小的改进,这可能会导致更多的事情按照您期望的方式工作。 The improvements all relate to “betterness” – the way the compiler decides which of two overloads is better for a given argument. 所有的改进都与“更好”有关–编译器决定两个重载中的哪个重载对于给定参数更好。

One place where you might notice this (or rather stop noticing a problem!) is when choosing between overloads taking nullable value types. 您可能会注意到这一点(或更确切地说,不再注意到问题!)的地方是在采用可空值类型的重载之间进行选择。 Another is when passing method groups (as opposed to lambdas) to overloads expecting delegates. 另一个是将方法组(而不是lambda)传递给期望委托的重载时。 The details aren't worth expanding on here – just wanted to let you know! 详细信息不值得在此处进行扩展–仅想让您知道!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决此错误“无法将lambda表达式转换为类型'string',因为它不是委托类型” - How to Solve this Error “Cannot convert lambda expression to type 'string' because it is not a delegate type” 如何解决此错误“因为它不是委托类型,所以无法将lambda表达式转换为'string'类型” - How to resolve this Error “Cannot convert lambda expression to type 'string' because it is not a delegate type” 错误:无法将lambda表达式转换为类型 - error: cannot convert lambda expression to type 无法转换Lambda表达式 - Cannot convert lambda expression 如何将表达式转换为 lambda 表达式? - How do I convert an Expression to a lambda expression? 如何将对象的实例化为lambda表达式 - How can I convert the instantiation of an object to a lambda expression MVC:DropdownListFor错误“无法将lambda表达式转换为类型'string',因为它不是委托类型” - MVC: DropdownListFor error “cannot convert lambda expression to type 'string' because it is not a delegate type” ASP.NET MVC 错误无法将 lambda 表达式转换为类型 'string' 因为它不是委托类型 - ASP.NET MVC error cannot convert lambda expression to type 'string' because it is not a delegate type 我如何从字符串中创建动态lambda表达式? - How i can make dynamic lambda Expression from string? 无法将lambda表达式转换为类型'string',因为它不是具有NotifyOfPropertyChange的委托类型 - Cannot convert lambda expression to type 'string' because it is not a delegate type With NotifyOfPropertyChange
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM