Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &&;不能应用于bool_C#_Asp.net Mvc - Fatal编程技术网

C# &&;不能应用于bool

C# &&;不能应用于bool,c#,asp.net-mvc,C#,Asp.net Mvc,我需要检查列表中的元素是否包含值true。我当前收到一个错误&&无法应用于bool或service 此时条件与服务.FirstOrDefault(p=>p.IsPrimaryBusinessLine==true): 服务 public class Service : BaseEntity { public int ServiceID { get; set; } public int RequestID { get; set; } publi

我需要检查列表中的元素是否包含值true。我当前收到一个错误&&无法应用于bool或service

此时
条件与服务.FirstOrDefault(p=>p.IsPrimaryBusinessLine==true)

服务

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

您可以使用Linq任何方法

例如:

services.Any(p => p.IsPrimaryBusinessLine == true)

您可以使用Linq任何方法

例如:

services.Any(p => p.IsPrimaryBusinessLine == true)

FirstOrdefault
返回与条件匹配的第一项,我想您想知道是否存在这样的服务,因为您在
中使用了它如果
,那么就不要使用
FirstOrdefault
,而是
任何

if (services != null && services.Any(p => p.IsPrimaryBusinessLine == true))
{

}

FirstOrdefault
返回与条件匹配的第一项,我想您想知道是否存在这样的服务,因为您在
中使用了它如果
,那么就不要使用
FirstOrdefault
,而是
任何

if (services != null && services.Any(p => p.IsPrimaryBusinessLine == true))
{

}

您正试图将其用作
bool

services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true)
但这并不能解析为
bool
,而是解析为
服务的一个实例。关于那项服务你想查什么?它存在吗?从你正在做的事情来看,可能是这样的:

services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true) != null
可简化为:

services.Any(p => p.IsPrimaryBusinessLine == true)

但是你需要检查一些东西。表达式本身需要解析为
bool
您试图将其用作
bool

services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true)
但这并不能解析为
bool
,而是解析为
服务的一个实例。关于那项服务你想查什么?它存在吗?从你正在做的事情来看,可能是这样的:

services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true) != null
可简化为:

services.Any(p => p.IsPrimaryBusinessLine == true)

但是你需要检查一些东西。表达式本身需要解析为
bool

最适合您的情况:

if (services != null && services.Count != 0 && services.Any(p => p.IsPrimaryBusinessLine))
{

}

针对您的情况的最佳方法:

if (services != null && services.Count != 0 && services.Any(p => p.IsPrimaryBusinessLine))
{

}
试试这个:

if (services != null && services.Any(p => p.IsPrimaryBusinessLine))
试试这个:

if (services != null && services.Any(p => p.IsPrimaryBusinessLine))
如果(services!=null&&services.Count!=0&&services.Any(p=>p.isprimary businessline))
services.FirstOrDefault(p=>p.isprimary businessline==true)
返回
services
实例,请这样尝试。您需要再对其进行一次比较,以获得
&
运算符所需的
bool
实例。如果(services!=null&&services.Count!=0&&services.Any(p=>p.isprimary businessline))
services.FirstOrDefault(p=>p.isprimary businessline==true)
返回
服务
实例。您需要再比较一次,以获得
&
运算符所需的
bool
实例。