简体   繁体   English

NHibernate Linq请求。不支持所有方法

[英]NHibernate Linq request .All method not supported

I would use a .All method in my request, but it seems, it isn't supported. 我会在请求中使用.All方法,但似乎不支持该方法。 I have a parameterList, wich contains elements with a value and a name (like a dictionary) and others things. 我有一个parameterList,其中包含带有值和名称的元素(如字典)和其他东西。 And Parameters, a list of elements with Value and Name. 和Parameters,具有值和名称的元素列表。 All my element in the first list must exist in the second. 我在第一个列表中的所有元素都必须存在于第二个列表中。 The request I would use is : 我将使用的请求是:

linq.Where(u => (u.ParametersList.All(param =>
     (Parameters.Any(p =>
          p.Value== param.Value && p.Name== param.Name)))));

If you have an idea for use something else than the .All, I listen to you :) I tried 如果您有使用.All以外的其他方法的想法,我会听你的:)我尝试过

!u.ParametersList.Any(param =>
     !(Parameters.Any(p =>
          p.Value== param.Value && p.Name== param.Name)));

but I guess Nhibernate don't make differences 但我想Nhibernate不会有所作为

I tried also 我也尝试过

List<System.Tuple<String, String>> ParamTuples = Parameters.Select(p => new System.Tuple<String, String>(p.Value, p.Name)).ToList();

So, ParamTuples the elements of my second list 因此,ParamTuples将第二个列表中的元素

linq = linq.Where(url => (url.ParametersList.Any(param =>
     ParamTuples.Any(p => p.Item1 == param.Value && p.Item2 == param.Name))));

But it didn't worked neither. 但这都不起作用。 Those methods are not supported. 不支持这些方法。

just to give the idea you'll need to count the parameter matches per containing element id and match them with the filterparameter count 只是为了给出一个想法,您需要计算每个包含元素id的参数匹配项,并将其与filterparameter计数进行匹配

class Parameter
{
    public virtual Entity Parent { get; set; }
    public virtual string Name { get; set; }
    public virtual string Value { get; set; }
}

ICriteria filter;
foreach(var param in parameterList)
{
    var crit = Expression.And(Expression.Eq("Name", param.Name), Expression.Eq("Value", param.Value);
    filter = (filter == null) ? crit : Expression.Or(filter, crit);
}
var subquery = QueryOver.Of<Parameter>()
    .Where(filter)
    .Select(Projections.Group("Parent.Id"));
    .Where(Restrictions.Eq(Projections.Count<Parameter>(p => p.Id), parameterList.Count));

var results = QueryOver.Of<Entity>()
    .WuithSubquery.WhereProperty(e => e.Id).IsIn(subquery)
    .List();

Update: as from your answer the above matches all but you want any (off the top of my head) 更新:从您的答案上面匹配所有,但您想要任何(我的头上)

var predicate = PredicateBuilder.False<Parameter>();
foreach (var param in Parameters)
{
    predicate = predicate.Or(p => p.Name == param.Name && p.Value == param.Value);
}
// building (u => u.ParametersList.Any(predicate))
var u = Expression.Parameter(typeof(User), "u");
var parametersproperty = Expression.Property(u, "ParametersList");
var anyCall = Expression.Call(parametersproperty, typeof(Queryable).Getmethod("Any"), predicate);

var lambda = Expression.Lambda<User, bool>(u, anyCall);

linq = linq.Where(lambda);

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

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