If statement cannot implicitly convert type to bool?
You are missing a =
. The =
is assignment, and comparison is ==
if (s4 == null) {
System.Console.WriteLine("Student Not Found");
} else {
System.Console.WriteLine($"{s4.FirstName} {s4.LastName} {s4.Major?.Description}");
var db = new AppEfDbContext();
}
Error on if statement - cannot implicitly convert type to 'bool'
c# equality operator is ==
and not =
:
if (thecitytype == "City1")
Cannot implicitly convert type 'string' to 'bool' [If statement]
z.Login is a string
validation is to make that the field has no value it receives null.
Do it like this:
Edit: Updated this section to also look for duplicates
public static ValidationResult validaUsuariosNaLista(Reuniao item)
{
var requeridos = item.Requeridos.Any(x => string.IsNullOrWhiteSpace(x.Login));
var informados = item.Informados.Any(x => string.IsNullOrWhiteSpace(x.Login));
var opcionais = item.Opcionais .Any(x => string.IsNullOrWhiteSpace(x.Login));
//does every item have a value?
if (requeridos || informados || opcionais)
return new ValidationResult(Resources.Validations.ValidaUsuarioMesmaLista);
//are all of the items unique?
if (item.Requeridos.Count() + item.Informados.Count() + item.Opcionais.Count() >
item.Requeridos.Concat(item.Informados).Concat(item.Opcionais).Distinct().Count)
{
//some items are duplicated
}
return ValidationResult.Success;
}
And, for fun, to avoid repeating code:
public static ValidationResult validaUsuariosNaLista(Reuniao item)
{
var HasEmptyValue = s => s.Any(x => string.IsNullOrWhiteSpace(x.Login));
//does every item have a value?
if (HasEmptyValue(item.Requeridos) || HasEmptyValue(item.Informados) || HasEmptyValue(item.Opcionais))
return new ValidationResult(Resources.Validations.ValidaUsuarioMesmaLista);
//are all of the items unique?
if (item.Requeridos.Count() + item.Informados.Count() + item.Opcionais.Count() >
item.Requeridos.Concat(item.Informados).Concat(item.Opcionais).Distinct().Count)
{
//some items are duplicated
}
return ValidationResult.Success;
}
Though I'm not 100% on how well type inference will work here... I'd have to see what the compiler makes of it, and I don't have your types to test with. But at worst, you'd just have to be explicit on the var
declaration.
Cannot implictly convert type int to bool
i % 2
is of type int, not bool, C# cannot automatically make the conversion, so simply try:
i % 2 != 0
How to resolve the error: Cannot implicitly convert type `long' to `bool'?
if
statements expect bool
check, in your case a % 2
returns just a long
number because a
is of type long
.
You just need to write a boolean expression
in your if
statement.
if (a % 2 != 0)
Why did I get an exception Cannot implicitly convert type 'bool' to 'long?' when using the LINQ Sum method and how to fix it?
Indeed, Sum requires numbers values and not boolean evaluation results.
You need first to filter using Where or Select (not relevant here) then Sum:
var sum = values.Where(x => x != null && x > 0).Sum();
I added the null check because the collection is type of decimal?
.
Else you need to write that but this is less speed optimized:
var sum = values.Sum(x => x != null && x > 0 ? x : 0);
Using a selector for Sum is for example usefull when having classes, structs or tuples:
var sum = controls.Sum(control => control.Width);
Related Topics
Getting HTML Body Content in Winforms Webbrowser After Body Onload Event Executes
Return HTML from ASP.NET Web API
How to Verify Text Is Bold Using Selenium on an Angular Website with C#
Seeking Clarification on Apparent Contradictions Regarding Weakly Typed Languages
ASP.NET Adding Class to Current Menuitem
Set Gridview Backcolor to Color from Datatable
Remove CSS Class in Code Behind
How to Call Code Behind Server Method from a Client Side JavaScript Function
JavaScript Date to C# via Ajax
How to Call an ASP.NET C# Method Using JavaScript
Webbrowser Control IE8 Compatibility Mode On/Off Switch
Wpf Application That Only Has a Tray Icon
What Is Linq and What Does It Do