C# Return 如果字符串不同于使用 LINQ 的字符串列表中的任何字符串,则为 true

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

如何使用 LINQ 获得与以下代码相同的结果?

它的作用是,如果一个字符串不同于字符串列表中的任何一个字符串,它将 return 为真。

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