簡體   English   中英

如何通過StartsWith在2個列表之間進行linq-to-hibernate

[英]How linq-to-nhibernate between 2 lists by StartsWith

我需要通過NHibernate 4運行此查詢:

var filterList = new List<string> { "1" , "2" } ;

var q = SessionInstance.Query<Book>()
          .Where(x => filterList.Any(s => s.StartsWith(x.Code)));
var list = q.ToList();

但是此消息有例外: Specified method is not supported.

堆棧跟蹤 :

at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource)
   at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree)
   at NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process()
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
   at NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
   at NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.DefaultQueryProvider.Execute[TResult](Expression expression)
   at Remotion.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

這沒有實現,但是我們可以將WHERE 條件轉換為OR表達式:

var q = SessionInstance
    .Query<Book>()
    //.Where(x => filterList.Any(s => s.StartsWith(x.Code)))
    .Where(x => x.Code.StartsWith("1")
             || x.Code.StartsWith("2")
    )
    ;

顯然,這不是動態的。 我們必須命名所有 OR語句,我們不能使用filterList ...除非我們尋求LinqKit幫助

並創建一種自動化的方法。 我們將需要這些using語句:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using LinqKit;

namespace MyNamespace
{
    public static partial class Ext
    {
        ...

擴展本身:

public static Expression<Func<T, bool>> AnyStarts<T>(
    this IList<string> startsWithValues, Expression<Func<T, string>> property)
{
    var nothingToDo = startsWithValues == null
                      || startsWithValues.Count == 0
                      || property == null;
    // nothing to process here, return null
    if (nothingToDo)
    {
        return null;
    }
    // first value ...
    string firstValue = startsWithValues.First();
    Expression<Func<T, bool>> predicate = 
        x => property.Invoke(x).StartsWith(firstValue);
    Expression<Func<T, bool>> result = predicate.Expand();

    if (startsWithValues.Count == 1)
    {
        // first value is enough
        return result;
    }

    // let's append Or if there are many string values
    for (int index = 1; index < startsWithValues.Count; index++)
    {
        string nextValue = startsWithValues[index];

        predicate = x => result.Invoke(x)
                         || property.Invoke(x).StartsWith(nextValue);

        result = predicate.Expand();
    }
    return result;
}

我們可以這樣稱呼它:

var filterList = new List<string> { "1" , "2" }; // or more

var q = SessionInstance
    .Query<Book>()
    //.Where(x => filterList.Any(s => s.StartsWith(x.Code)))
    .Where(filterList.AnyStarts<Contact>(x => x.Code))
    ;

我們將收到任何代碼以“ 1”,“ 2”,...開頭的書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM