使用 Firebird 的“SUBSTR”函数查询

query using function “SUBSTR” of Firebird

我需要使用带有 , or . I may do it with SQL 的 firebird SUBSTR 函数编写 NHibernate 查询,但这些将是我最后的选择。

有人有什么想法吗?以下示例查询是我的 NHibernate 查询产生的 SQL:

SELECT *
FROM LANCAMENTO
WHERE SUBSTR(LAN_CD_CONTA, 1, 13) = :paramCd_Conta

使用 Linq,声明一个 SubStr 扩展方法:

using NHibernate.Linq;

...

public static class CustomLinqExtensions
{
    [LinqExtensionMethod("SUBSTR")]
    public static string SubStr(this string dummy, int start, int length)
    {
        // No need to implement it in .Net, unless you wish to call it
        // outside IQueryable context too.
        throw new NotImplementedException("This call should be translated " +
            "to SQL and run db side, but it has been run with .Net runtime");
    }
}

然后在您的实体上使用它:

session.Query<Lancamento>()
    .Where(l => l.CdConta.SubStr(1, 13) == cdConta)
    .ToList();

请注意,尝试在其用法中不引用实体的情况下使用它会导致使用 .Net 运行时对其进行评估,而不是将其转换为 SQL。