English 中文(简体)
实体框架4.0 .Create Query<T>和命令 例外
原标题:Entityframework 4.0 .CreateQuery<T> and OrderBy exception

我认为这在4.0中是固定的。 我有这种方法。

public IQueryable<T> All(Expression<Func<T,object>> sort)
    {
       return EntityContext.CreateQuery<T>(EntityName).AsQueryable<T>().OrderBy(sort);
    }

下面是例外。

Unable to cast the type System.Int32 to type System.Object . LINQ to Entities only supports casting Entity Data Model primitive types. Source is System.Data.Entity

Any idea how to fix this or if there s any workaround This happens with every call when the order is not a string datatype

IQueryable<Blog> sortedAll = _repository.All(x => x.Title);

由于标题是指令 罚款。 但它没有其他数据类型。

自2006年以来 在我看到马克·斯之前,我提出了这一“坏”的解决办法。 我认为我是这样说的。

public IQueryable<T> All(Expression<Func<T,object>> sort)
    {

        var expresssionType = sort.Body.GetType();
       string propertyName= expresssionType == typeof(System.Linq.Expressions.UnaryExpression) ? ((MemberExpression)((UnaryExpression)sort.Body).Operand).Member.Name : ((MemberExpression)sort.Body).Member.Name;
        var items=   EntityContext.CreateQuery<T>(EntityName).AsQueryable<T>();
        var type = typeof(T);
        var expressionProperty = type.GetProperty(propertyName);
        var exPressionparameter = Expression.Parameter(type, "p");
        var propertyAccess = Expression.MakeMemberAccess(exPressionparameter, expressionProperty);
        var orderByExp = Expression.Lambda(propertyAccess, exPressionparameter);
        Expression resultExp = Expression.Call(typeof(Queryable), "OrderBy", new Type[] { type, expressionProperty.PropertyType }, items.Expression, Expression.Quote(orderByExp));
        return items.AsQueryable().Provider.CreateQuery<T>(resultExp);
    }
最佳回答

我可以改变时间,但可以把工作推到周围? i.e.

public IQueryable<T> All<TValue>(Expression<Func<T,TValue>> sort)
{
    return EntityContext.CreateQuery<T>(EntityName)
           .AsQueryable<T>().OrderBy<T,TValue>(sort);
}

以及

IQueryable<Blog> sortedAll = _repository.All(x => x.SomeProp);

?

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签