LINQ to Entities-无法将'System.DateTime'强制转换为orderBy的'System.Object'类型 [英] LINQ to Entities - cannot cast 'System.DateTime' to type 'System.Object' in orderBy

查看:151
本文介绍了LINQ to Entities-无法将'System.DateTime'强制转换为orderBy的'System.Object'类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Expression中传递的日期按日期对实体的IQueryable进行排序. Func< T,object >>并收到错误:无法将类型'System.Nullable'1'转换为类型'System.Object'.LINQto Entities仅支持转换实体数据模型原语类型."该实体具有一个可为空的datetime属性,我正在尝试对该属性进行排序:

I am trying to order an IQueryable of entities by date from a passed in Expression< Func< T, object>> and am getting the error: "Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types." The entity has a nullable datetime property on it on which I am trying to sort:

示例:(其中e.Date是可为空的DateTime)

Example: (where e.Date is a nullable DateTime)

Expression<Func<T,object>> sorter = (e) => e.Date;
IOrderedQueryable<T> sortedData = data.OrderBy(sorter);

提前谢谢!

推荐答案

我编写了一个简单的类,用于在运行时基于lambda表达式对实体进行排序.

I wrote a simple class for ordering entities based on a lambda expression at runtime.

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

namespace DataModeling
{
    public class QueryOrderer<TEntity>
        where TEntity : class
    {
        private LambdaExpression defaultSortExpression;
        private Dictionary<string, LambdaExpression> orderFieldLookup;

        public QueryOrderer()
        {
            orderFieldLookup = new Dictionary<string, LambdaExpression>();
        }

        public void AddOrderMapping<TProp>(string fieldName, Expression<Func<TEntity, TProp>> selector)
        {
            orderFieldLookup[fieldName] = selector;
        }

        public void SetDefaultSortExpression<TProp>(Expression<Func<TEntity, TProp>> selector)
        {
            defaultSortExpression = selector;
        }

        public IQueryable<TEntity> GetOrderedEntries(string field, bool isDescending, IQueryable<TEntity> entries)
        {
            return orderEntries(entries, field, isDescending);
        }

        private IQueryable<TEntity> orderEntries(IQueryable<TEntity> entries, string fieldName, bool isDescending)
        {
            dynamic lambda = getOrderByLambda(entries, fieldName);
            if (lambda == null)
            {
                return entries;
            }
            if (isDescending)
            {
                return Queryable.OrderByDescending(entries, lambda);
            }
            else
            {
                return Queryable.OrderBy(entries, lambda);
            }
        }

        private dynamic getOrderByLambda(IQueryable<TEntity> entries, string fieldName)
        {
            if (!String.IsNullOrWhiteSpace(fieldName) && orderFieldLookup.ContainsKey(fieldName))
            {
                return orderFieldLookup[fieldName];
            }
            else
            {
                return defaultSortExpression;
            }
        }
    }
}

您可以通过首先设置所有字段来使用此类:

You use this class by initially setting up all of the fields:

QueryOrderer<User> orderer = new QueryOrderer<User>();
orderer.SetDefaultSortExpression(u => u.FullName);
orderer.AddOrderMapping("UserId", u => u.UserId);
orderer.AddOrderMapping("Name", u => u.FullName);
orderer.AddOrderMapping("Email", u => u.Email);
orderer.AddOrderMapping("CreatedOn", u => u.CreatedOn);

...

var users = orderer.GetOrderedEntries("CreatedOn", isDescending: false, context.Users);

此代码的一个不错的功能是,它可以完美地处理查找值.例如,如果您尝试使用描述而不是键进行排序,那么在建立排序表达式时可以使用外部上下文:

I nice feature of this code is that it handles look-up values perfectly. For instance, if you're trying to sort using the description rather than a key, you can use the outer context when building up the sort expression:

orderer.AddOrderMapping("UserType", 
    u => context.UserTypes
                .Where(t => t.UserTypeId == u.UserTypeId)
                .Select(t => t.Description)
                .FirstOrDefault());

实体框架足够聪明,只需将子查询直接折叠到外部查询中即可.

Entity Framework is smart enough to just fold the sub-query right into the outer query.

这篇关于LINQ to Entities-无法将'System.DateTime'强制转换为orderBy的'System.Object'类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆