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

时间:2017-12-12 02:37:28

标签: c# linq .net-4.0

如何使用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;
}

2 个答案:

答案 0 :(得分:4)

cPickle.dumps(obj, protocol=cPickle.HIGHEST_PROTOCOL)

答案 1 :(得分: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));
    }