2012-05-09 49 views
2

我想知道是否有任何可能的方式在DataView上應用RowFilter提供單字符匹配?匹配DataView RowFilter中的單個字符?

例如DataView.RowFilter =「喜歡‘A_BCD%’SOME_COL;

其中下劃線表示的Oracle SQL單字符匹配,並且%是0或多個字符的通配符

顯然的RowFilter只支持通配符。 (* or %)在匹配表達式的開頭或結尾,我唯一的想法是迭代遍歷行並刪除與正則表達式不匹配的行,但這不是理想的。

任何幫助或想法將不勝感激! 謝謝

回答

3

你可以使用LINQ到數據集做這個

class Program 
{ 

    static void Main(string[] args) 
    { 
     DataTable dt = new DataTable("test"); 
     dt.Columns.Add("SOME_COL", typeof(string)); 

     dt.Rows.Add("AABCDEFG"); 
     dt.Rows.Add("AZBCDEFG"); 
     dt.Rows.Add("AZZBCDEFG"); 
     dt.Rows.Add("ABC"); 

     Regex r = new Regex("A\\wBCDE"); 

     DataView dv 
      = (from t in dt.AsEnumerable() 
       where r.IsMatch(t.Field<string>("SOME_COL")) 
      select t).AsDataView(); 

     foreach(DataRowView row in dv) 
     { 
      Console.WriteLine(row[0]); 
     } 


    } 


} 

輸出

AABCDEFG 
AZBCDEFG 
Press any key to continue . . . 
+0

這個偉大的工程!謝謝。 Linq真的很有用:) – russdot

1

另外,

DataView.RowFilter = "Substring(SOME_COL, 1, 1) = 'A' and Substring(SOME_COL, 3, 3) = 'BCD';" 
+0

有趣的 - 我不知道你可以寫一個字符串表達式來評估行過濾器。最後是否有不匹配的括號的原因? – russdot

+1

@russdot從複製和粘貼測試中錯別字。固定。 – Loathing

相關問題