代码之家  ›  专栏  ›  技术社区  ›  Shawn Mclean

子级上的linq扩展方法

  •  1
  • Shawn Mclean  · 技术社区  · 16 年前

    我创建了一个linq扩展 here . 但很显然,它不适用于对孩子的询问。如下代码所示:

    return (from p in repository.GetResources()
                    where p.ResourceNames.Any(r => r.Name.WhereIn(arg => arg.Name, "PartialWord"))
                    select p).ToList();
    

    我要完成的是resourcenames是一个entitycollection,我需要检查子级的属性“name”是否包含单词“partialword”。

    其中工作方式如下:

    repository.GetResources().WhereIn({CODEHERE});
    

    但当放在孩子身上时,它就不起作用了。有什么特别的方法来设置我的分机吗?下面是:

    public static class QueryableExtensions
    {
        private static Expression<Func<TElement, bool>> GetWhereInExpression<TElement, TValue>(Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values)
        {
            ParameterExpression p = propertySelector.Parameters.Single();
            if (!values.Any())
                return e => false;
    
            var equals = values.Select(value => (Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
            var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal));
    
            return Expression.Lambda<Func<TElement, bool>>(body, p);
        }
    
        /// <summary> 
        /// Return the element that the specified property's value is contained in the specifiec values 
        /// </summary> 
        /// <typeparam name="TElement">The type of the element.</typeparam> 
        /// <typeparam name="TValue">The type of the values.</typeparam> 
        /// <param name="source">The source.</param> 
        /// <param name="propertySelector">The property to be tested.</param> 
        /// <param name="values">The accepted values of the property.</param> 
        /// <returns>The accepted elements.</returns> 
        public static IQueryable<TElement> WhereIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, params TValue[] values)
        {
            return source.Where(GetWhereInExpression(propertySelector, values));
        }
    
        /// <summary> 
        /// Return the element that the specified property's value is contained in the specifiec values 
        /// </summary> 
        /// <typeparam name="TElement">The type of the element.</typeparam> 
        /// <typeparam name="TValue">The type of the values.</typeparam> 
        /// <param name="source">The source.</param> 
        /// <param name="propertySelector">The property to be tested.</param> 
        /// <param name="values">The accepted values of the property.</param> 
        /// <returns>The accepted elements.</returns> 
        public static IQueryable<TElement> WhereIn<TElement, TValue>(this IQueryable<TElement> source, Expression<Func<TElement, TValue>> propertySelector, IEnumerable<TValue> values)
        {
            return source.Where(GetWhereInExpression(propertySelector, values));
        } 
    }
    
    1 回复  |  直到 16 年前
        1
  •  2
  •   Craig Stuntz    16 年前

    推定 ResourceName.Name 是一个字符串:

    You don't need any extension for the string version of .Contains() in L2E . (你 做 仅适用于L2EV.1中的列表版本)。你应该能够做到:

    return (from p in repository.GetResources()
            where p.ResourceNames.Any(r => r.Name.Contains("PartialWord"))
            select p).ToList();