2010-07-29 33 views
0

我需要從DataTable中檢索一組「Top n」行數,其中表行按Ordered By「Column X」排序,但只有當行的「列X」值大於提供的比較值。這是我到目前爲止有:如何比較LINQ查詢中的字符串列值?

EnumerableRowCollection query = from history in dt.AsEnumerable() 
           where history.Field<string>("Column X") > "Value-To-Compare") 
           orderby history.Field("Column X") 
           select history; 

但我一直收到「操作員‘>’不能被應用到類型‘串’和‘串’的操作數」

有什麼想法?

fuzzlog

回答

0

這項工作?

EnumerableRowCollection query = from history in dt.AsEnumerable() 
           where String.Compare(history.Field<string>("Column X"), "Value-To-Compare") > 0 
           orderby history.Field("Column X") 
           select history; 
0

伊恩,做了詭計,謝謝。

對不起,在您給出解決方案後回答我的問題,但我需要增強您的答案,使其與原始請求的100%相匹配。

...檢索一組的 「TOP N」 的 行數...

完整的答案應該是這樣的:

EnumerableRowCollection query = (
            from history in dt.AsEnumerable() 
            where String.Compare(history.Field<string>("Column X"), "Value-To-Compare") > 0 
            orderby history.Field("Column X") 
            select history 
           ).Take(n); 

其中 「N」 是「Top n」中指定的行數

再次感謝,

fuzzlog