2014-02-26 73 views
1

我有這樣的查詢。DataTable Linq where子句大寫比較

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; 

回答

6

你不說你是「不工作」的意思,但你可以嘗試:

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

這將工作(*),即使其中一個值是零,而你使用ToUpper()會拋出NullReferenceException

(*)「工作」,如果你想文化不敏感的序數比較。如果不是,則爲StringComparison參數使用不同的值。

+0

工程就像一個魅力。非常感謝。 –

+0

是的,你是對的。我得到的錯誤是NullReferenceException。它很混亂,但是當你提到null的價值時,我明白了。 –