SelectExcept removing joined tables

c# dynamic-linq

Question

I created a SelectExcept based on https://stackoverflow.com/a/27205784/7468628 using dynamic linq.

Code:

public static IQueryable SelectExcept<TSource, TResult>(this IQueryable<TSource> source, List<string> excludeProperties)
    {

        var sourceType = typeof(TSource);
        var allowedSelectTypes = new Type[] { typeof(string), typeof(ValueType) };
        var sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => allowedSelectTypes.Any(t => t.IsAssignableFrom(((PropertyInfo)p).PropertyType))).Select(p => ((MemberInfo)p).Name);
        var sourceFields = sourceType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => allowedSelectTypes.Any(t => t.IsAssignableFrom(((FieldInfo)f).FieldType))).Select(f => ((MemberInfo)f).Name);

        var selectFields = sourceProperties.Concat(sourceFields).Where(p => !excludeProperties.Contains(p)).ToArray();

        var dynamicSelect =
                string.Format("new( {0} )",
                        string.Join(", ", selectFields));

        return selectFields.Count() > 0
            ? source.Select(dynamicSelect)
            : Enumerable.Empty<TSource>().AsQueryable<TSource>();
    }

This works well with fields, however when I use SelectExcept on an object that is an object from a database and is connected with another table, I miss the combined value. How can I continue to maintain this combined value?

1
1
5/23/2017 11:53:30 AM

Popular Answer

Well, I've discovered the solution, so if you're wondering how to repair it, here it is:

 public static IQueryable SelectExcept<TSource, TResult>(this IQueryable<TSource> source, List<string> excludeProperties)
    {

        var sourceType = typeof(TSource);
        var allowedSelectTypes = new Type[] { typeof(string), typeof(ValueType), typeof(object) };
        var sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => allowedSelectTypes.Any(t => t.IsAssignableFrom(((PropertyInfo)p).PropertyType))).Select(p => ((MemberInfo)p).Name);
        var sourceFields = sourceType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => allowedSelectTypes.Any(t => t.IsAssignableFrom(((FieldInfo)f).FieldType))).Select(f => ((MemberInfo)f).Name);
        var selectFields = sourceProperties.Concat(sourceFields).Where(p => !excludeProperties.Contains(p)).ToArray();

        var dynamicSelect =
                string.Format("new( {0} )",
                        string.Join(", ", selectFields));

        return selectFields.Count() > 0
            ? source.Select(dynamicSelect)
            : Enumerable.Empty<TSource>().AsQueryable<TSource>();
    }

To gather the fields of your own specified classes, you required typeof(object) for the chosen types.

1
2/27/2017 10:49:10 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow