查询期间的NotSupported Exception。 [英] NotSupported Exception during the query.

查看:41
本文介绍了查询期间的NotSupported Exception。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我想为我的MVC应用程序制作类似通用存储库的smth。我使用VS 2010 + EF 4.所以我创建了属性:
protected ObjectQuery< T>实体集
{
     get {return _entitySet ?? (_entitySet = _entities.CreateObjectSet< T>(EntitySetName));返回当前实体的一组对象的方法。
我有从集合中返回一些实体的方法:
公共虚拟IQueryable< T> ListEntity(表达式< Func< T,bool>> condition,int skip,int count,Expression< Func< T,object>> orderCondition,bool isDescending)
    &NBSP;&NBSP;   {
    &NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; return(isDescending)?
    &NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;  (EntitySet.Where(condition).OrderByDescending(orderCondition).Skip(skip).Take(count))
    &NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   :( EntitySet.Where(condition).OrderBy(orderCondition).Skip(skip).Take(count));    &NBSP;&NBSP;  }
问题是,当我将参数传递给此方法时 - 这些参数是表达式,f.e。
表达式< Func< T,object>> orderCondition
{
    得到     {
   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; var param = Expression.Parameter(typeof(T)," item");
   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;返回Expression.Lambda< Func< T,object>>   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP; (Expression.Convert(Expression.Property(param," UserID"),typeof(object)),param);
    表达式< Func< T,bool>>条件
{
   得到    {
         var searchWhere = PredicateBuilder.True< T>();
        searchWhere = searchWhere.And(e => e.GetType()。GetProperty(searchForm.SearchField)
                                                .GetValue(e,null).ToString()。包含(searchForm.Keyword));
      &NBSP;&NBSP;&NBSP; .......有些逻辑在这里.....
    }
},

我没有支持异常。似乎实体框架不知道任何标准的.net方法。例外情况发生在ToString()方法,Invoke(),Convert()......和其他。
所以问题是 - 是否有可能将一些复杂的参数传递给EF查询,这些参数需要先执行。我可以调用EntitySet.ToList()然后所有这些参数都很好用。但如果我的表中有300000个项目,EntitySet.ToList()将从DB获取所有项目!这是非常缓慢...任何帮助将不胜感激。

Hi everybody!
I want to make smth like generic repository for my MVC application. I use VS 2010 + EF 4. So I created property:
protected ObjectQuery<T> EntitySet
{
     get { return _entitySet ?? (_entitySet = _entities.CreateObjectSet<T>(EntitySetName)); }
}

which returns set of objects of current entity.
I have method for returning some entities from set:
public virtual IQueryable<T> ListEntity(Expression<Func<T, bool>> condition, int skip, int count, Expression<Func<T, object>> orderCondition, bool isDescending)
        {
            return (isDescending) ?
                (EntitySet.Where(condition).OrderByDescending(orderCondition).Skip(skip).Take(count))
                       : (EntitySet.Where(condition).OrderBy(orderCondition).Skip(skip).Take(count));
        }
The problem is that, when I pass parameters to this method - these params are expressions, f.e.
Expression<Func<T, object>> orderCondition
{
     get
     {
            var param = Expression.Parameter(typeof(T), "item");
            return Expression.Lambda<Func<T, object>>
            (Expression.Convert(Expression.Property(param, "UserID"), typeof(object)), param);
     }
}
or lambda-expressions f.e.
Expression<Func<T, bool>> condition
{
    get
    {
         var searchWhere = PredicateBuilder.True<T>();
        searchWhere = searchWhere.And(e => e.GetType().GetProperty(searchForm.SearchField)
                                                            .GetValue(e, null).ToString().Contains(searchForm.Keyword));
         .......Some logic goes here.....
    }
},

I have NOTSUPPORTED EXCEPTION. Seems that Entities Framework doesn't know any of standard .net methods. Exceptions goes at ToString() method, Invoke(), Convert()... and other.
So the question is - is there a possibility to pass to EF queries some complex parameters, which need to be executed first.
I can call EntitySet.ToList() and then all these params are working great. But if there are 300000 items in my table, EntitySet.ToList() will get them all from DB! Ths is extremely slow... Any help would be appreciated.

推荐答案

我认为你根本不需要转换节点,这里是一个样本适用于Northwind:


I don't think you need the Convert node at all, here's a sample that works against Northwind:

using System;

namespace EFExprTreeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var ctx = new NorthwindEntities();
            var repository = new NWRepository<Order>(ctx, "Orders");

            foreach (var item in repository.ListEntity(o => o.OrderID <= 10255, 2, 1, o => o.CustomerID, true))
            {
                Console.WriteLine(item.OrderID);
            }
        }
    }
}





using System;
using System.Data.Objects;
using System.Linq;
using System.Linq.Expressions;

namespace EFExprTreeTest
{
    class NWRepository<T> where T : class
    {

        public NWRepository(NorthwindEntities entities, string entitySetName)
        {
            _entities = entities;
            EntitySetName = entitySetName;
        }

        public string EntitySetName { get; private set;}

        public virtual IQueryable<T> ListEntity(Expression<Func<T, bool>> condition, 
                                                int skip, 
                                                int count, 
                                                Expression<Func<T, object>> orderCondition, 
                                                bool isDescending)
        {
            return (isDescending) ?
                EntitySet.Where(condition).OrderByDescending(this.orderCondition).Skip(skip).Take(count)
                       : (EntitySet.Where(condition).OrderBy(this.orderCondition).Skip(skip).Take(count));
        }

        Expression<Func<T, object>> orderCondition
        {
            get
            {
                var param = Expression.Parameter(typeof(T), "item");
                return Expression.Lambda<Func<T, object>>
                (/*Expression.Convert(*/Expression.Property(param, "CustomerID")/*, typeof(object))*/, param);
            }
        }


        protected ObjectQuery<T> EntitySet
        {
            get { return _entitySet ?? (_entitySet = _entities.CreateObjectSet<T>(EntitySetName)); }
        }

        private ObjectQuery<T> _entitySet;
        private NorthwindEntities _entities;
    }
}



希望有所帮助,

Jonathan Aneja
项目经理,实体框架团队


Hope that helps,

Jonathan Aneja
Program Manager, Entity Framework team


这篇关于查询期间的NotSupported Exception。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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