Reputation: 8681
I need to check if an element in the list contains value true. I m currently getting an error && cannot be applied to bool or service
at this condition & services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true)
:
var services = serviceRepository
.GetServicesByRequestID(newReqeustViewModel.RequestID);
if (services != null &&
services.Count != 0 &&
services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true))
Service
public class Service : BaseEntity
{
public int ServiceID { get; set; }
public int RequestID { get; set; }
public string BusinessLineCode { get; set; }
public string BusinessLine { get; set; }
public bool IsPrimaryBusinessLine { get; set; }
public int ContractLineSLAID { get; set; }
}
Upvotes: 0
Views: 102
Reputation: 460238
FirstOrdefault
returns the first item that matches the condition, i reckon you want to know if there is such a service because you use it in an if
, then dont use FirstOrdefault
but Any
:
if (services != null && services.Any(p => p.IsPrimaryBusinessLine == true))
{
}
Upvotes: 2
Reputation: 5062
Try this:
if (services != null && services.Any(p => p.IsPrimaryBusinessLine))
Upvotes: 0
Reputation: 1132
The best way for your situation:
if (services != null && services.Count != 0 && services.Any(p => p.IsPrimaryBusinessLine))
{
}
Upvotes: 0
Reputation: 219016
You're trying to use this as a bool
:
services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true)
But that doesn't resolve to a bool
, it resolves to an instance of Service
. What are you trying to check about that service? That it exists? From what you're doing, that could be something like:
services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true) != null
Which can be simplified to:
services.Any(p => p.IsPrimaryBusinessLine == true)
But you need to check for something. The expression itself needs to resolve to a bool
.
Upvotes: 2
Reputation: 3071
You could use the Linq Any method.
For example:
services.Any(p => p.IsPrimaryBusinessLine == true)
Upvotes: 1