2017-05-23 58 views
0

我需要檢查列表中的元素是否包含值true。我米目前得到一個錯誤& &不能應用到bool或服務&&不能應用於布爾

在此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)) 

服務

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

嘗試這樣'如果(服務= NULL && services.Count = 0 && services.Any (p => p.IsPrimaryBusinessLine))' – Nino

+0

'services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true)'返回'服務'實例。您需要再次比較一下''&&'操作符所需的'bool'實例。 –

回答

1

你可以使用LINQ的任何方法。

例如:

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

FirstOrdefault返回符合條件的第一個項目,我想你想知道是否有這樣的服務,因爲你在if使用它,然後不使用FirstOrdefaultAny

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

} 
2

你試圖以此爲bool

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

但是這並不解決爲bool,它解決了Service的一個實例。你想嘗試檢查關於該服務?它存在嗎?從你在做什麼,這可能是這樣的:

services.FirstOrDefault(p => p.IsPrimaryBusinessLine == true) != null 

這可以簡化爲:

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

但你需要檢查的東西。表達式本身需要解析爲bool

0

您的具體情況最好的方法:

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

} 
0

試試這個:!

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