LINQ检查布尔值

时间:2018-02-08 06:51:44

标签: c# linq

我有一个List AffiliateConfigurations

public class AffiliateConfiguration
{
    public int AffiliateConfigurationId { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}

以下示例数据:

(1,"AddOn",true)
(2,"DummyEmail",false)
(3,"ProviderType",true)

基本上我想执行一些操作,如果我的List包含名称等于ProviderType且其IsActive值包含false的任何项目。 我试过这个,这是正确的方法吗?

if (AffiliateConfigurations.Any(x => x.Name.Equals("ProviderType")) && !AffiliateConfigurations.FirstOrDefault(x => x.Name.Equals("ProviderType")).IsActive)
 {
    //perform some action
 }

4 个答案:

答案 0 :(得分:7)

下面这个条件不简单吗?您不必检查两次,因为您可以直接访问IsActive属性。

if (AffiliateConfigurations.Any(x => x.Name.Equals("ProviderType") && !x.IsActive))
{

}

答案 1 :(得分:3)

您可以直接查看i = 0 ip_file.readline() for line in ip_file: line = line.strip() year[i] = int(line[0:4]) month[i] = int(line[5:7]) day[i] = int(line[8:10]) hour[i] = int(line[11:13]) time[i] = (datetime(year[i],month[i],day[i],hour[i])-datetime(1970, 1, 1)).total_seconds() i += 1

IsActive

答案 2 :(得分:0)

您正在执行两项验证: - 首先检查集合中是否至少有一个项目名称与“ProvidersType”匹配。 - 您再次遍历您的集合以查找此类项目(它将在第一次验证通过后存在)并检查它是否处于活动状态。

您可以在单次运行中执行此操作:     if(AffiliateConfigurations.Any(x => x.Name ==“ProviderType”&& x.IsActive == false / *或“!x.IsActive”* /))     {     //执行一些动作     }

如果您的“AffiliateConfigurations”是IEnumerable,那么可以“过滤”您的收藏集的linq方法将接受Func<TypeBeingFiltered, bool>Predicate<TypeBeingFiltered>(例如Any,{{1} },WhereFirstOrDefault,...),只要在结尾返回Single值,就可以将任何类型的代码块作为这些参数的一部分。

答案 3 :(得分:0)

只需像#John Woo所说的那样使用这个条件

if(isset($_POST['searchq']) && $_POST['searchq'] != "")
{
    if(isset($_POST['searchopt']) && $_POST['searchopt'] != "")
    {
        $escsearch = mysqli_real_escape_string($conn, $_POST['searchq']);
        $searchval = preg_replace('#[^a-z 0-9]#i', '', $escsearch);
        $opt = $_POST['searchopt'];

        switch($opt)
        {
            case "dpt":
            $query = "SELECT * FROM coi_system where department LIKE '%$searchval%'";
            $res = mysqli_query($conn, $query) or die(mysqli_error());
            $count = mysqli_num_rows($res);
            if($count > 0)
            {
                $output = "$count results for <strong>$escsearch</strong>.";

                while($row = mysqli_fetch_array($res))
                {
                    $accountcode = $row['accountcode'];
                    $department = $row['department'];
                    $person_in_charge = $row['person_in_charge'];
                    $project_title = $row['project_title'];
                    $objective = $row['objective'];
                    $how_to_do = $row['how_to_do'];
                    $activities = $row['activities'];
                    $project_started = $row['project_started'];
                    $project_completed = $row['project_completed'];
                    $target_cost_saving = $row['target_cost_saving'];
                    $costsaving_afterjustification = $row['costsaving_afterjustification'];
                    $costsaving_monthly = $row['costsaving_monthly'];

                    echo "<table>
                            <tr>
                                <td><div>Account Code</div></td>
                                <td><div>Department</div></td>
                                <td><div>Person in charge</div></td>
                                <td><div>Project title</div></td>
                                <td><div>Objective</div></td>
                                <td><div>How To Do</div></td>
                                <td><div>Activities</div></td>
                                <td><div>Project Started</div></td>
                                <td><div>Project Completed</div></td>
                                <td><div>Target Cost Saving</div></td>
                                <td><div>Cost Saving After Justification</div></td>
                                <td><div>Cost Saving Monthly</div></td>
                            </tr>
                            <tr>
                                <td><div>$accountcode</div></td>
                                <td><div>$department</div></td>
                                <td><div>$person_in_charge</div></td>
                                <td><div>$project_title</div></td>
                                <td><div>$objective</div></td>
                                <td><div>$how_to_do</div></td>
                                <td><div>$activities</div></td>
                                <td><div>$project_started</div></td>
                                <td><div>$project_completed</div></td>
                                <td><div>$target_cost_saving</div></td>
                                <td><div>$costsaving_afterjustification</div></td>
                                <td><div>$costsaving_monthly</div></td>
                            </tr>
                         </table>"; }
            }else{
                $output = "<p>No records found.</p>"; 
            }
            break;
        }
    }
}

循环通过AffiliateConfigurations只有一次,在你的方法中,它必须循​​环两次一个用于任何部分,一个用于FirstOrDefault部分......