简体   繁体   English

如何为nHibernate正确翻译LINQ string.Contains(“ substring%”)?

[英]How to translate LINQ string.Contains(“substring%”) correctly for nHibernate?

The following code is used to give nHibernate a way to translate my C# LINQ code to proper SQL-statements. 以下代码用于为nHibernate提供一种将我的C#LINQ代码转换为适当的SQL语句的方法。 What I want is to have 3 SQL-statements like: 我想要的是具有3个SQL语句,例如:

"Select * From HITable Where Series LIKE %substring"

or 要么

"Select * From HITable Where Series LIKE substring%"

or 要么

"Select * From HITable Where Series LIKE sub%ng"

The problem is that when nHibernate gets this it translates StartsWith, EndsWith, and Contains in all 3 cases to %substring%, ie wildcard is put both in the beginning and in the end of the substring. 问题在于,当nHibernate收到此消息时,它将在所有3种情况下将StartsWith,EndsWith和Contains转换为%substring%,即通配符同时放在子字符串的开头和结尾。 What is the proper way solve this? 解决此问题的正确方法是什么?

    private static Expression<Func<HIProduct, bool>> CheckWildCardPosition(string rangeFrom)
    {
        if (rangeFrom.StartsWith(SqlWildCardAnyValue.ToString()))
        {
            return hip => hip.ProductId.Series.StartsWith(rangeFrom);
        }
        else if (rangeFrom.EndsWith(SqlWildCardAnyValue.ToString()))
        {
            return hip => hip.ProductId.Series.EndsWith(rangeFrom);
        }
        else
        {
            return hip => hip.ProductId.Series.Contains(rangeFrom);
        }
    }

I discovered that the problem when using StartsWith/EndsWith is that you have to remove the '%'-character, otherwise you actually will get 2 '%'-characters. 我发现使用StartsWith / EndsWith时出现的问题是必须删除'%'字符,否则实际上将获得2个'%'字符。 This is because nHibernate translates StartsWith/EndsWith to a '%'. 这是因为nHibernate将StartsWith / EndsWith转换为'%'。 So, a first draft of the solution in code would look like the following: 因此,该解决方案的代码初稿如下所示:

    private static Expression<Func<HIProduct, bool>> CheckWildCardPosition(string serie)
    {
        char[] wildCard = new char[] { SqlWildCardAnyValue };

        if (serie.StartsWith(SqlWildCardAnyValue.ToString()))
        {
            if (serie.EndsWith(SqlWildCardAnyValue.ToString()))
            {
                return hip => hip.ProductId.Series.Contains(serie.Trim(wildCard));
            }
            return hip => hip.ProductId.Series.EndsWith(serie.TrimStart(wildCard));
        }
        else if (serie.EndsWith(SqlWildCardAnyValue.ToString()))
        {
            return hip => hip.ProductId.Series.StartsWith(serie.TrimEnd(wildCard));
        }
        else
        {
            string[] split = serie.Split(wildCard);
            return hip => hip.ProductId.Series.StartsWith(split[0]) && hip.ProductId.Series.EndsWith(split[1]);
        }
    }

Remember that this is a first draft, and I know the code could be refactored to more effective code, but in a first step this solves my problem. 请记住,这是初稿,我知道可以将代码重构为更有效的代码,但是第一步可以解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM