c# - Linq 查询 : does this array contain this string?

标签 c# linq

有更好的方法吗?

    public bool IsServiceRunning(string serviceName)
    {
        string[] services =  client.AllServices();
        return (from s in services
                where s.Equals(serviceName, StringComparison.InvariantCultureIgnoreCase)
                select s).Count() > 0;
    }

比较中不区分大小写很重要。

最佳答案

使用 Any linq扩展方法:

public bool IsServiceRunning(string serviceName)
{
    string[] services =  client.AllServices();
    return services.Any(s => 
        s.Equals(serviceName, StringComparison.InvariantCultureIgnoreCase));
}

这种方式一旦找到匹配项,就会停止执行。

关于c# - Linq 查询 : does this array contain this string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2097974/

相关文章:

c# - 我们可以用 C# 为 MS-Word 编写宏吗

c# - 从串行端口读取和存储字节

c# - Func<T> 是如何隐式转换为 Expression<Func<T>> 的?

c# - 动态 LINQ 在 IQueryable 上聚合为单个查询

c# - 如果创建出错,删除数据库

c# - 在 EF6 中将匿名类型转换为 IEnumerable<>

c# - ViewModel中的引用ResourceDictionary

c# - 我应该如何从自定义对象的 List<T> 中提取不同值的集合?

c# - 使用 LINQ 查找两个数据对象之间的差异

mysql - 让我的 SQL 查询以 LINQ 形式显示