简体   繁体   English

每个属性的GroupBy Lamda表达式生成器

[英]Expression builder for GroupBy Lamda for each property

I want to populate the itemsource of a ComboBox with items from my List, depending on which property from T is selected. 我想用列表中的项目填充ComboBox的itemsource,具体取决于从T中选择了哪个属性。

The statement should be like: foreach property which is a string, select the values of the property, make distinct. 该语句应类似于:foreach属性(它是一个字符串),选择属性的值,使其与众不同。

    public Dictionary<string, List<string>> CreateSuggestionsLists<T>(List<T> data)
    {
        var queryableData = data.AsQueryable();
        var paramExp = Expression.Parameter(typeof(T), "left");
        foreach (var pi in typeof(T).GetProperties().Where(p => p.PropertyType == typeof(string)))
        {
            var callExpr = Expression.MakeMemberAccess(paramExp, pi);
            var lambdaExpr = Expression.Lambda(callExpr) ;
            // From here on it goes wrong!!!
            var comleteExpr = lambdaExpr as Expression<Func<T, bool>>;
            var compiledExpr = comleteExpr.Compile();

            var res = data.Select(compiledExpr).Distinct().ToList();
            // add to results ...
        }

        return null;
    }

The problem seems to be the casting from the lambda expression to prepare for compilation. 问题似乎是从lambda表达式进行转换以准备进行编译。

Thank you for your help. 谢谢您的帮助。

First of all you need to provide paramExp to lambda. 首先,您需要为lambda提供paramExp Secondly there is generic version of Lamda method which is just easier to use. 其次,有通用版本的Lamda方法,它更易于使用。 Finally, you don't need to compile expression when you use IQueryable . 最后,使用IQueryable时不需要编译表达式。 You created queryableData variable and didn't use it. 您创建了queryableData变量,但没有使用它。

Here is code: 这是代码:

    public Dictionary<string, List<string>> CreateSuggestionsLists<T>(List<T> data)
    {
        var queryableData = data.AsQueryable();
        var paramExp = Expression.Parameter(typeof(T), "left");
        foreach (var pi in typeof(T).GetProperties().Where(p => p.PropertyType == typeof(string)))
        {
            var callExpr = Expression.MakeMemberAccess(paramExp, pi);
            var lambdaExpr = Expression.Lambda<Func<T, bool>>(callExpr, paramExp);

            var res = queryableData.Select(lambdaExpr).Distinct().ToList();
            // add to results ...
        }

        return null;
    }

I think you should check if the casting result is not null : 我认为您应该检查转换结果是否不为null:

          public Dictionary<string, List<string>> CreateSuggestionsLists<T>(List<T> data)
    {
        IQueryable<T> queryableData = data.AsQueryable();
        ParameterExpression paramExp = Expression.Parameter(typeof(T), "left");

        foreach (PropertyInfo pi in typeof(T).GetProperties().Where(p => p.PropertyType == typeof(string)))
        {
            MemberExpression callExpr = Expression.MakeMemberAccess(paramExp, pi);
            LambdaExpression lambdaExpr = Expression.Lambda(callExpr);
            // From here on it goes wrong!!!
            if (!(lambdaExpr is Expression<Func<T, bool>> comleteExpr)) continue;

            Func<T, bool> compiledExpr =  comleteExpr.Compile();

            List<bool> res = data.Select(compiledExpr).Distinct().ToList();

            // add to results ...
        }

        return null;
    }

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

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