【问题标题】:how to compare a string column value in a LINQ query?如何比较 LINQ 查询中的字符串列值?
【发布时间】:2010-07-31 17:26:36
【问题描述】:

我需要从 DataTable 中检索一组“TOP n”行数,其中表行按“Column X”排序,但前提是行的“Column X”值大于提供的比较价值。这是我目前所拥有的:

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

但我不断收到“运算符'>'不能应用于'string'和'string'类型的操作数”

有什么想法吗?

模糊日志

【问题讨论】:

    标签: linq-to-sql


    【解决方案1】:

    这行得通吗?

    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;
    

    【讨论】:

      【解决方案2】:

      伊恩,成功了,谢谢。

      很抱歉在您给出解决方案后回答了我的问题,但我需要改进您的回答,使其与原始请求 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”中指定的行数

      再次感谢,

      模糊日志

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 2021-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多