DataTable Linq where子句以大写字母比较

时间:2014-02-26 16:49:17

标签: 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;

1 个答案:

答案 0 :(得分:6)

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

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

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

(*)“工作”,如果你想要一个文化不敏感的序数比较。如果不是,请为StringComparison参数使用不同的值。