【问题标题】:C# No Overload for method 'ToString' takes 1 argument方法“ToString”的 C# 无重载需要 1 个参数
【发布时间】:2014-10-20 16:04:55
【问题描述】:

这是我的错误:

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) 我有两个错误,最后都是一样的:

无法将 lambda 表达式转换为委托类型“System.Func”,因为块中的某些返回类型不能隐式转换为委托返回类型。 AND 不能将类型“字符串”隐式转换为“布尔”。

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

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

然后我有错误:

方法 'ToString' 没有重载需要 1 个参数。

这不是我第一次遇到这种错误,但我仍然不知道如何解决这个问题..

提前致谢。 问候。

编辑 1:

这就是我所在的地方。

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,而不是好的。

【问题讨论】:

  • 什么是LibelléTOTAvant
  • Libelle_TOT 的类型?
  • 只需检查类型并根据它们修改您的代码。
  • Libelle_TOT 和 LibelléTOTAvant 是 VarChar(50) 数据类型。
  • 你应该使用类似LibelléTOTAvant = cap.Any(c => c.Libelle_TOT=="YourString")的东西。你也可以'LibelléTOTAvant = cap.Select(c => c.Libelle_TOT).FirstOrDefault()'

标签: c# sql wpf database linq


【解决方案1】:

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

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

所以你应该这样做

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

例如,或任何其他需要返回布尔值的东西。

如果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'

或者在这种情况下,你可以这样做

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

【讨论】:

  • 我已经尝试过您的代码 LibelléTOTAvant = cap.Any(c => !string.IsNullOrEmpty(c.Libelle_TOT)),但我遇到了比以前相反的错误,无法将类型“bool”隐式转换为字符串'
  • @Kraenys 是的,所以在该行的末尾添加一个.ToString()(但您将拥有truefalse,这可能不是您想要的),或者使用最后一个我的答案中的代码。
  • 确实,这不是我想要的。我不明白你的意思是什么“?”和“:
  • @Kraenys 它是一个三元运算符(如果...那么...否则)
  • @Kraenys 我编辑了编辑,在这种情况下可能更容易解决。
猜你喜欢
  • 1970-01-01
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 2021-11-04
相关资源
最近更新 更多