Kraenys
Kraenys

Reputation: 297

C# No Overload for method 'ToString' takes 1 argument

this is where is my error:

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"
            });
        }
    }
}

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

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'.

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

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

and then I have the error:

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..

Thanks in advance. Greetings.

Edit 1:

This is where I am.

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"
            });

        }
    }
}

Both of

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

and

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

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

Upvotes: 0

Views: 1468

Answers (1)

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

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

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>.

So you should do

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

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

and if LibelléTOTAvant is a string

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

EDIT

for example

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'

Upvotes: 3

Related Questions