C#方法'ToString'的无重载采用1个参数 [英] C# No Overload for method 'ToString' takes 1 argument

查看:1065
本文介绍了C#方法'ToString'的无重载采用1个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的错误所在:

foreach (var donneesDump in don)
{
    if (cap.Any(c => c.PMRQTOTM == donneesDump.PMRQTOTM))
    {
        if(!cap.Any(d => d.Libelle_TOT == donneesDump.Libelle_TOT))
        {
            cnn.Resultat.Add(new Resultat
            {
                NomTable = "CapitalisationActuelle",
                Groupe_D_alerte = donneesDump.Groupe_Alerte,
                NomChamp = "PMRQTOTM",
                TOTMPMRQ = donneesDump.PMRQTOTM,
                SiModifie = "Libelle TOT",
                LibelléTOTAvant = cap.Any(c => c.Libelle_TOT),
                LibelléTOTApres = donneesDump.Libelle_TOT,
                Remarque = "Ajoute"
            });
        }
    }
}

在线LibelléTOTAvant = cap.Any(c => c.Libelle_TOT) 我有两个错误,最后都相同:

On line LibelléTOTAvant = cap.Any(c => c.Libelle_TOT) I have two errors, which are finally the same:

无法将lambda表达式转换为委托类型'System.Func',因为该块中的某些返回类型不能隐式转换为委托返回类型. AND无法将类型'string'隐式转换为'bool'.

Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type. AND Cannot implicitly convert type 'string' to 'bool'.

我试图做一个ToString()方法来解决这个问题,像这样:

I have tried to do a ToString() Method to solve the problem, like this:

LibelléTOTAvant = ToString(cap.Any(c => c.Libelle_TOT)),

然后出现错误:

方法"ToString"的任何重载都不需要1个参数.

No Overload for method 'ToString' takes 1 argument.

这不是我第一次遇到这种错误,但是我仍然找不到解决方法.

It isn't the first time I have this kind of error, but I still don't find how solve this..

先谢谢了. 问候.

这是我的位置.

foreach (var donneesDUMP in don)
{
    if (cap.Any(c => c.PMRQTOTM == donneesDUMP.PMRQTOTM))
    {
        if(!cap.Any(c => c.Libelle_TOT == donneesDUMP.Libelle_TOT))
        {
            cnn.Resultat.Add(new Resultat
            {
                NomTable = "CapitalisationActuelle",
                Groupe_D_alerte = donneesDUMP.Groupe_Alerte,
                NomChamp = "PMRQTOTM",
                TOTMPMRQ = donneesDUMP.PMRQTOTM,
                SiModifie = "Libelle TOT",
                LibelléTOTAvant = cap.Select(c => c.Libelle_TOT).FirstOrDefault(),
                //LibelléTOTAvant = cap.Any(c => !string.IsNullOrEmpty(c.Libelle_TOT))
                //? cap.Select(x => x.Libelle_TOT).First(l => !string.IsNullOrEmpty(l))
                //: " ",
                LibelléTOTApres = donneesDUMP.Libelle_TOT,
                Remarque = "Modifie"
            });

        }
    }
}

两者

LibelléTOTAvant = cap.Select(c => c.Libelle_TOT).FirstOrDefault(),

LibelléTOTAvant = cap.Any(c => !string.IsNullOrEmpty(c.Libelle_TOT))
                        ? cap.Select(x => x.Libelle_TOT).First(l => !string.IsNullOrEmpty(l))
                        : " ",

有效.但是每次我遇到一个问题时,可能都是.First()和.FirstOrDefault(). 它总是写第一个Libelle_TOT,而不是好的.

works. But each time I have one problem, probably with .First() and .FirstOrDefault(). It always write the first Libelle_TOT, not the good one.

推荐答案

我相信Libelle_TOT是一个字符串(来自Cannot implicitly convert type 'string' to 'bool'.错误消息)

I believe Libelle_TOT is a string (from the Cannot implicitly convert type 'string' to 'bool'. error message)

cap.Any(c => c.Libelle_TOT)在这种情况下没有意义,因为Any应该以Func<T, bool>作为参数(返回布尔值),并且您传递Func<T, string>.

cap.Any(c => c.Libelle_TOT) doesn't make sense in this case, as Any should have a Func<T, bool> as argument (something returning a bool) and you pass a Func<T, string>.

所以你应该做

cap.Any(c => !string.IsNullOrEmpty(c.Libelle_TOT))

例如,

或其他任何需要返回布尔值的东西.

for example, or anything else needed which would return a bool.

,如果LibelléTOTAvant是字符串

cap.Any(c => !string.IsNullOrEmpty(c.Libelle_TOT))
   ? <something which is a string>
   : <something else which is a string>

编辑

例如

cap.Any(c => !string.IsNullOrEmpty(c.Libelle_TOT))
   ? cap.Select(x => x.Libelle_TOT).First(l => !string.IsNullOrEmpty(l))
   : 'No Label'

或者在这种情况下,您可以

or in this case, you could do

   cap.Select(x => x.Libelle_TOT).FirstOrDefault(l => !string.IsNullOrEmpty(l)) ?? 'No Label'

这篇关于C#方法'ToString'的无重载采用1个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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