无法转换类型“System.String”输入“System.Object”。 LINQ to Entities 仅支持转换实体数据模型基元类型

发布于 2024-09-08 23:11:48 字数 697 浏览 4 评论 0 原文

我正在使用 EF 4,但当我尝试订购列表时,它给了我错误。

Unable to cast the type 'System.String' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

这是我通过输入属性名称来获取表达式的代码,下面的示例获取客户名称

var param = Expression.Parameter(typeof(Customer), "N");

var sortExpression = Expression.Lambda<Func<T, object>>
            (Expression.Convert(Expression.Property(param, "Name"), typeof(object)), param);

,我的 EF 代码是

GetObjectSet<T>().AsQueryable().OrderBy(sortExpression).Skip(0).Take(5);

我知道这是 EF 的某种转换问题,因为它可以在没有 EF 的情况下工作,但当我将其与 EF 连接起来。是否有解决方法或其他原因,因为我不想使用 LINQ。

I am using EF 4 but it is giving me error when I try to order my list.

Unable to cast the type 'System.String' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

This is my code to get the experssion by entering the property name, example below get the Customer Name

var param = Expression.Parameter(typeof(Customer), "N");

var sortExpression = Expression.Lambda<Func<T, object>>
            (Expression.Convert(Expression.Property(param, "Name"), typeof(object)), param);

And my EF code is

GetObjectSet<T>().AsQueryable().OrderBy(sortExpression).Skip(0).Take(5);

I know it is some sort of casting problem with EF because it works without the EF but it gives me this error when i hook it up with EF. Is there a work around or something because i don't want to use LINQ.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-09-15 23:11:48

我遇到了同样的问题,并通过以下代码解决:

IQueryable<T> result = DbSet.AsQueryable<T>();
var classPara = Expression.Parameter(typeof(T), "t");
var pi = typeof(T).GetProperty(sortPara);
result = result.Provider.CreateQuery<T>(
                    Expression.Call(
                        typeof(Queryable),
                        "OrderBy",
                        new Type[] { typeof(T), pi.PropertyType },
                        result.Expression,
                        Expression.Lambda(Expression.Property(classPara , pi), classPara ))
                    );

I've encountered the same problem, and solved by the following code:

IQueryable<T> result = DbSet.AsQueryable<T>();
var classPara = Expression.Parameter(typeof(T), "t");
var pi = typeof(T).GetProperty(sortPara);
result = result.Provider.CreateQuery<T>(
                    Expression.Call(
                        typeof(Queryable),
                        "OrderBy",
                        new Type[] { typeof(T), pi.PropertyType },
                        result.Expression,
                        Expression.Lambda(Expression.Property(classPara , pi), classPara ))
                    );
阪姬 2024-09-15 23:11:48

我相信这个答案解决了您想要做的事情以最简单的方式。

I believe this answer addresses what you're trying to do in the easiest possible way.

執念 2024-09-15 23:11:48

我遇到了同样的问题,解决它的方法是:

using System; 
 using System.Collections.Generic; 
 using System.Linq; 
 using System.Linq.Expressions; 


public class GenericSorter<T> {
public IEnumerable<T> Sort(IEnumerable<T> source, string sortBy, string sortDirection)
{
    var param = Expression.Parameter(typeof(T), "item");

    var sortExpression = Expression.Lambda<Func<T, object>>
        (Expression.Convert(Expression.Property(param, sortBy), typeof(object)), param);

    switch (sortDirection.ToLower())
    {
        case "asc":
            return source.AsQueryable<T>().OrderBy<T, object>(sortExpression);
        default:
            return source.AsQueryable<T>().OrderByDescending<T, object>(sortExpression);

    } 
}

}

然后你可以在执行查询时调用它:

var entity = nameof(DBOEntity.Property);
var order = "asc";
var query = dc.EntityDataSet.AsExpandable().OrderByDynamic(entity, order);

我希望这对你有帮助!

I had the same problem, and the method that solved it was:

using System; 
 using System.Collections.Generic; 
 using System.Linq; 
 using System.Linq.Expressions; 


public class GenericSorter<T> {
public IEnumerable<T> Sort(IEnumerable<T> source, string sortBy, string sortDirection)
{
    var param = Expression.Parameter(typeof(T), "item");

    var sortExpression = Expression.Lambda<Func<T, object>>
        (Expression.Convert(Expression.Property(param, sortBy), typeof(object)), param);

    switch (sortDirection.ToLower())
    {
        case "asc":
            return source.AsQueryable<T>().OrderBy<T, object>(sortExpression);
        default:
            return source.AsQueryable<T>().OrderByDescending<T, object>(sortExpression);

    } 
}

}

Then you can call it when you perform the query as:

var entity = nameof(DBOEntity.Property);
var order = "asc";
var query = dc.EntityDataSet.AsExpandable().OrderByDynamic(entity, order);

I hope this helps you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文