简体   繁体   中英

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

How do I achieve the same result as code below using LINQ?

What it do is if a string is different from any of the string in a list of strings, it will return 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)

Try like this;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM