c# - DataTable Linq where 子句与大写比较

标签 c# linq

我有这样的查询。

 DataTable products = ...
 redirect_str = ...

 IEnumerable<DataRow> productsQuery =
                    from product in products.AsEnumerable()
                    where product.Field<String>("url") == redirect_str
                    select product;

它工作正常。

但是我如何在不考虑大小写的情况下比较 product.Field("url") 和 redirect_str .我试过了,但没用。

 IEnumerable<DataRow> productsQuery =
                    from product in products.AsEnumerable()
                    where product.Field<String>("url").ToUpper() == redirect_str.ToUpper()
                    select product;

最佳答案

你没有说“不起作用”是什么意思,但你可以尝试:

...
where String.Equals(product.Field<String>("url"),  
    redirect_str, 
    StringComparison.OrdinalIgnoreCase)
...

即使其中一个值为 null,这也会起作用 (*),而您使用 ToUpper() 会抛出 NullReferenceException

(*) 如果您想要进行不区分文化的序数比较,请使用“work”。如果不是,请为 StringComparison 参数使用不同的值。

关于c# - DataTable Linq where 子句与大写比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22047884/

相关文章:

c# - linq to xml 中标记的内容

c# - 字符串或二进制数据将在 Linq 中被截断

c# - 使用 CanUserAddRows 为 DataGrid 的新行设置样式

c# - 如何使用 Migradoc PDF 库打开现有的 PDF 文件

c# - List.First() 是否返回一个新实例?

c# - Linq to Entities 选择具有最大日期的数据?

c# - LINQ 选择新错误。无法访问列表

C# 使用 DataReader 和 CSV writer 批量/ block 导出 SQL Server 大表

c# - Visual Studio 2017 RC - C# UWP 项目 - 无法创建类

c# - 如何在 C# 中将 ImmutableArray<DerivedClass> 转换为 ImmutableArray<BaseClass> ,反之亦然?