使用动态列名从Entity Framework中选择不同的列

时间:2017-08-02 09:12:30

标签: c# entity-framework

我正在寻找一种获取不同值列表的方法,用于表格的一列。我需要制作一个可重复使用的方法。

这是我到目前为止所尝试的,但它不起作用:

IEnumerable<string> GetDistinctValues<T>(string columnName)
{
    T.Select(m => m.ColumnName).Distinct().ToList();
}

所需的解决方案应该是EF对象的扩展方法。

我已尝试过这篇文章Dynamically select columns in runtime using entity framework,但它仅适用于单个记录而非列表。

3 个答案:

答案 0 :(得分:3)

我在Linq.Dynamic看到的唯一问题是自2013年以来没有更新,项目已经死了

我会通过扩展来处理它,并通过缓存提高反射性能(这里没有详细说明)

<强>扩展

public static class QueryableExtensions
{
    public static IReadOnlyCollection<TResult> GetDistinctValuesForProperty<T, TResult>(this IQueryable<T> query, Expression<Func<T, TResult>> propertyAccess)
    {
        return SelectDistinct(query, propertyAccess).ToList();
    }

    public static IReadOnlyCollection<object> GetDistinctValuesForProperty<TSource>(this IQueryable<TSource> query, string propertyName)
    {
        var unboundFuncType = typeof(Func<,>);
        var unboundExprType = typeof(Expression<>);

        var sourceType = typeof(TSource); // TSource

        var resultType = typeof(TSource)
            .GetProperty(propertyName)
            .PropertyType; // TResult

        // Func<TSource, TResult>
        var funcType = unboundFuncType.MakeGenericType(new [] { sourceType, resultType });

        // Expression<Func<TSource, TResult>>
        var expressionType = unboundExprType.MakeGenericType(new [] { funcType });

        // Instance of Expression<Func<TSource, TResult>>, for example x => x.Name
        var propertyAccess = typeof(StringExtensions)
            .GetMethod(nameof(StringExtensions.AsPropertyExpression), new[] { typeof(string) })
            .MakeGenericMethod(new [] { sourceType, resultType })
            .Invoke(null, new object[] { propertyName });

        // SelectDistinct query transform
        var selectDistinctMethod = typeof(QueryableExtensions)
            .GetMethod(nameof(QueryableExtensions.SelectDistinct), BindingFlags.NonPublic | BindingFlags.Static)
            .MakeGenericMethod(new [] { sourceType, resultType });

        // IQueryable<TSource> ==> IQueryable<TResult>
        var result = selectDistinctMethod.Invoke(null, new object[] { query, propertyAccess });

        // Cast to object via IEnumerable and convert to list
        return ((IEnumerable)result).Cast<object>().ToList();
    }

    private static IQueryable<TResult> SelectDistinct<TSource, TResult>(this IQueryable<TSource> query, Expression<Func<TSource, TResult>> propertyAccess)
    {
        return query.Select(propertyAccess).Distinct();
    }
}

public static class StringExtensions
{
    public static Expression<Func<T, TResult>> AsPropertyExpression<T, TResult>(this string propertyName)
    {
        var parameter = Expression.Parameter(typeof(T), "x");
        var property = typeof(T).GetProperty(propertyName);
        var body = Expression.MakeMemberAccess(parameter, property);
        return Expression.Lambda<Func<T, TResult>>(body, parameter);
    }
}

<强>用法:

public class Person
{
    public string Name { get; }
    public int Age { get; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

var people = new Person[]
{
    new Person("John", 25), new Person("Peter", 25), new Person("Sean", 25),
    new Person("John", 32), new Person("Peter", 32),
};

var query = people.AsQueryable();

var namePropertyExpression = "Name".AsPropertyExpression<Person, string>();
var agePropertyExpression = "Age".AsPropertyExpression<Person, int>();

// When you know the result type
var names1 = query.GetDistinctValuesForProperty(x => x.Name);
var ages1 = query.GetDistinctValuesForProperty(x => x.Age);

// When you know the result type, but you may want to reuse the property expression
var names2 = query.GetDistinctValuesForProperty(namePropertyExpression);
var ages2 = query.GetDistinctValuesForProperty(agePropertyExpression);

// When you just know the property name
var names3 = query.GetDistinctValuesForProperty("Name");
var ages3 = query.GetDistinctValuesForProperty("Age");

答案 1 :(得分:0)

最后我找到了解决方案。我需要包含对System.Linq.Dynamic的引用(由nuget下载),并使用&#34; Select&#34;接受String到引用列的方法。

# create data
m <- matrix(sample(x = 0:1,size = 200,replace = T),ncol = 10)
colnames(m) <- LETTERS[1:10]
m
# create cor matrix
res <- data.frame()
for(i in seq(ncol(m))){
  z <- m[,i]
  z <- apply(m,2,function(x){sum(x==z)/length(z)})
  res <- rbind(res,z)
}
colnames(res) <- colnames(m)
rownames(res) <- colnames(m)
res <- as.matrix(res)
res

并致电

using System.Linq.Dynamic;

public static async Task<IEnumerable<Object>> GetDistinctValuesForProperty<T>(this IQueryable<T> query, String PropertyName)
  {
  return await query.Select(PropertyName).Distinct().ToListAsync();
  }

由于日期时间类型

的播放问题,我需要使用数组vs ienumerable

答案 2 :(得分:0)

您可以使用Expressions

创建通用选择器方法
public static Func<T, T> SelectorFunc<T>(string[] columns) {
// input parameter "o"
var xParameter = Expression.Parameter(typeof(T), "o");

// new statement "new Data()"
var xNew = Expression.New(typeof(T));

// create initializers
var bindings = columns.Select(o => o.Trim())
 .Select(o =>
    {

        // property "Field1"
        var mi = typeof(T).GetProperty(o);

        // original value "o.Field1"
        var xOriginal = Expression.Property(xParameter, mi);

        // set value "Field1 = o.Field1"
    return Expression.Bind(mi, xOriginal);
        }
        );

    // initialization "new Data { Field1 = o.Field1, Field2 = o.Field2 }"
        var xInit = Expression.MemberInit(xNew, bindings);

        // expression "o => new Data { Field1 = o.Field1, Field2 = o.Field2 }"
        var lambda = Expression.Lambda<Func<T, T>>(xInit, xParameter);

       // compile to Func<Data, Data>
     return lambda.Compile();
}

使用它将是

T.Select( SelectorFunc<T>( new string[]{ "Column" } ) ).Distinct().ToList();

您还可以将其用于任何其他linq函数,例如

T.Select( SelectorFunc<T>( new string[]{ "Column" } ) ).Where();
T.Select( SelectorFunc<T>( new string[]{ "Column" } ) ).AsQueryable();

有关其他参考,您可以在此处查看完整的操作说明 LINQ : Dynamic select

相关问题