c# - 检查二维数组中的值

标签 c# performance linq

我有一个二维数组 int[][] 数据,我想看看该值是否包含值 45 例如

我创建了这行代码

bool contains = data.Where(x => x.Contains(45)).ToArray().Count() != 0 ? true : false;

但看起来有不必要的代码,我相信有更简单或更有效的方法来做到这一点

最佳答案

您可以使用 Any linq 扩展。

bool exists = data.SelectMany(x=>x).Any(x=>x == 45);

或者

bool exists = data.Any(x=>x.Any(s=>s == 45));

关于c# - 检查二维数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38824703/

相关文章:

c# - Microsoft ASP.NET 和 Web 工具 2015 - DNX 和 DNVM

performance - 将数据库放在专用服务器上时性能会变差吗?

c# - 从列表中获取月份名称和年份

Linq:包含多个表时的 Orderby

c# - 解决 WCF 错误 : The EncryptedKey clause was not wrapped with the required encryption token 'System.IdentityModel.Tokens.X509SecurityToken'

c# - SWIG:生成 C# 包装器时 SWIGTYPE_p_void 未定义

c# - 应用程序架构,实现这一目标的最佳方法

c# - 减少纬度和经度点数量的最快方法

performance - 为什么 Angular 2 比 Angular 1 有更好的性能?

c# Linq 关键字?