【问题标题】:Linq select row where columns nth character = xLinq选择第n个字符= x的行
【发布时间】:2015-02-26 14:48:37
【问题描述】:

我有一个遗留数据库,我需要在其中获取某种类型的所有行。该类型没有可供搜索的列,但幸运的是,有一个命名约定,当它是我想要的类型时,每个第三个字符都有一个破折号。我想我可以简单地这样做:

var sectionsList = (from sec in db.Query<Section>()
                                     where (sec.Name[2] == '-')
                                     select sec).ToList().Distinct();

但是我得到这个错误:

“System.NotSupportedException”类型的异常发生在 NHibernate.dll 但未在用户代码中处理 附加 信息:Char get_Chars(Int32)

我发现这看起来像同样的问题: LINQ to Entities does not recognize the method Int32 get_Item(Int32) 但是这种情况涉及与可以事先获取的本地值进行比较。我需要在搜索过程中遍历 Name 字符串。

【问题讨论】:

    标签: c# linq nhibernate


    【解决方案1】:

    我们应该使用Substring 方法:

    var sectionsList = 
      (
        from sec in db.Query<Section>()
        where sec.Name.Substring(2,1) == "-")
        select sec
      )
      .ToList().Distinct();
    

    这应该被转换成 DB 函数,如:substring(NAME, 2, 1) = '-'

    【讨论】:

    • 如果有帮助就太好了 ;) 享受很棒的 NHibernate ;)
    猜你喜欢
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多