簡體   English   中英

C#如果字符串與使用LINQ的字符串列表中的任何字符串不同,則返回true

[英]C# Return true if string is different from any of the string in a list of strings using LINQ

如何使用LINQ獲得與以下代碼相同的結果?

它的作用是,如果一個字符串與字符串列表中的任何一個字符串都不相同,它將返回true。

public static bool MasterPlantDifferentFromDetailPlant(string mrNumber)
{
    string masterPlant = t_MT_MTInfo.GetMaterialRequestPlant(mrNumber);
    List<string> detailPlants = t_MT_MTItem.GetPlants(mrNumber);
    bool differentPlant = false; 
    foreach (string plant in detailPlants)
    {
        if (string.Compare(masterPlant.Trim(), plant.Trim(), StringComparison.OrdinalIgnoreCase) != 0)
        {
            differentPlant = true;
            break;
        }
    }
    return differentPlant;
}
detailPlants.Any(p => string.Compare(masterPlant.Trim(), p.Trim(), StringComparison.OrdinalIgnoreCase) != 0)

這樣嘗試;

    public static bool MasterPlantDifferentFromDetailPlant(string mrNumber)
    {
        string masterPlant = t_MT_MTInfo.GetMaterialRequestPlant(mrNumber);
        List<string> detailPlants = t_MT_MTItem.GetPlants(mrNumber);
        return !detailPlants.All(x => string.Equals(masterPlant.Trim(), x.Trim(), StringComparison.OrdinalIgnoreCase));
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM