vendredi 3 novembre 2017

Type arguments cannot be inferred for generalized Expression variable

I'm getting the following error:

the type arguments for method 'Queryable.OrderBy<TSource,TKey>(
    IQueryable<TSource>, Expression<Func<TSource,TKey>>) 
cannot be inferred from the useage.

How would I either modify Me.GetOrder() or the declaration of myOrderByVariable so that it creates a variable that works as a parameter to the OrderBy() without using a custom Extension Method? This code is just a test. I need the variable to send to a foreign repository.

void Main()
{
    // Assemble
    string property = "sku";
    var propertyType = Product.GetType().GetProperty(property).GetType();
    var entityType = Product.GetType();

    // Act
    // TODO: begin part to change
    var myOrderByVariable = Me.GetOrder(Product.GetType(), propertyType, property );
    // end part to change

    // Assert
    Product.Where(p => p.custid == 1 && p.eventid == 0 && p.pbfeattext != null)
    .OrderBy( myOrderByVariable )
    .Skip(10).Take(10)
    .Dump();
}

public static class Me
{
    // TODO: begin part to change
    public static Expression<Func<TS, TK>> 
        GetOrder<TS,TK>(TS entity, TK propType, string propertyName)
    {
        //Create x=>x.PropName
        var propertyInfo = typeof(TS).GetProperty(propertyName);
        ParameterExpression arg = Expression.Parameter(typeof(TS), "x");
        MemberExpression property = Expression.Property(arg, propertyName);
        var selector = Expression.Lambda<Func<TS,TK>>(
                       property, new ParameterExpression[] { arg });

        return selector;
    }
    // end part to change
}





Aucun commentaire:

Enregistrer un commentaire