Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过StartWith在两个列表之间进行linq到nhibernate_C#_Linq_Nhibernate_Linq To Nhibernate - Fatal编程技术网

C# 如何通过StartWith在两个列表之间进行linq到nhibernate

C# 如何通过StartWith在两个列表之间进行linq到nhibernate,c#,linq,nhibernate,linq-to-nhibernate,C#,Linq,Nhibernate,Linq To Nhibernate,我需要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(); 未实现此功能,但我们可以将此类WHERE条件转换为或表达式:

我需要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();

未实现此功能,但我们可以将此类WHERE条件转换为或表达式:

var q = SessionInstance
    .Query<Book>()
    //.Where(x => filterList.Any(s => s.StartsWith(x.Code)))
    .Where(x => x.Code.StartsWith("1")
             || x.Code.StartsWith("2")
    )
    ;
以及扩展本身:

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;
}
公共静态表达式AnyStarts(
此IList启动时使用值,表达式属性)
{
var nothingToDo=startsWithValues==null
||startsWithValues.Count==0
||属性==null;
//此处无需处理,返回null
如果(无提示)
{
返回null;
}
//第一个值。。。
string firstValue=startsWithValues.First();
表达式谓词=
x=>property.Invoke(x).StartsWith(firstValue);
表达式结果=predicate.Expand();
if(startsWithValues.Count==1)
{
//第一个值就足够了
返回结果;
}
//如果有许多字符串值,让我们附加Or
对于(int index=1;indexresult.Invoke(x)
||property.Invoke(x).StartsWith(nextValue);
结果=predicate.Expand();
}
返回结果;
}
我们可以这样称呼它:

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))
    ;
var filterList=新列表{“1”,“2”};//或更多
var q=会话持续时间
.Query()
//.Where(x=>filterList.Any(s=>s.StartsWith(x.Code)))
.Where(filterList.AnyStarts(x=>x.Code))
;
我们将收到任何代码以“1”、“2”开头的书

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))
    ;