EF4 CTP5 - LINQ动态查询库抛出InvalidCastException [英] EF4 CTP5 - LINQ Dynamic Query Library throws InvalidCastException

查看:120
本文介绍了EF4 CTP5 - LINQ动态查询库抛出InvalidCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到EF4 CTP5,以前工作(使用CTP4)LINQ动态查询库引发了以下异常



无法投射类型为 System.Data.Entity.Infrastructure.DbQuery'键入'System.Linq.IQueryable`1 [KIT.TAM.Core.Entities.TravelAgent]'。



在下面的return语句中:

 命名空间System.Linq.Dynamic 
{
public static class DynamicQueryable
{
public static IQueryable< T>其中< T>(这个IQueryable< T>来源,字符串谓词,params对象[]值)
{
return(IQueryable< T>)其中((IQueryable)source,predicate,values);
}
}
}

是否有更新版本使用EF4 CTP5的图书馆?



感谢各位。

解决方案



我替换

  public static IQueryable< T>其中< T>(这个IQueryable< T>来源,字符串谓词,params对象[]值)
{
return(IQueryable< T>)其中((IQueryable)source,predicate,values);
}

with

  public static IQueryable< T>其中< T>(此IQueryable< T>源,字符串谓词,params对象[]值)
{
if(source == null)throw new ArgumentNullException(source);
if(predicate == null)throw new ArgumentNullException(predicate);
LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType,typeof(bool),谓词,值);
return source.Provider.CreateQuery< T>(
Expression.Call(
typeof(Queryable),Where,
new Type [] {source.ElementType},
source.Expression,Expression.Quote(lambda)));
}

这是基本相同的代码在

  public static IQueryable Where(this IQueryable source,string predicate,params object [] values)

,但将 source.Provider.CreateQuery()更改为 source.Provider.CreateQuery< T>



您还需要为静态方法 OrderBy< T>


With the upgrade to EF4 CTP5, the previously working (with CTP4) LINQ Dynamic Query Library throws the following exception

Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery' to type 'System.Linq.IQueryable`1[KIT.TAM.Core.Entities.TravelAgent]'.

on the return statement below:

namespace System.Linq.Dynamic
{
    public static class DynamicQueryable
    {
        public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values)
        {
            return (IQueryable<T>)Where((IQueryable)source, predicate, values);
        }
    }
}

Is there an updated version of the library that works with EF4 CTP5?

Thanks folks.

解决方案

Solved this one. In DynamicLibrary.cs:

I replaced

public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values)
{    
    return (IQueryable<T>)Where((IQueryable)source, predicate, values);
}

with

public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values)
{
    if (source == null) throw new ArgumentNullException("source");
    if (predicate == null) throw new ArgumentNullException("predicate");
    LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
    return source.Provider.CreateQuery<T>(
        Expression.Call(
            typeof(Queryable), "Where",
            new Type[] { source.ElementType },
            source.Expression, Expression.Quote(lambda)));
}

This is the basically the same code in

public static IQueryable Where(this IQueryable source, string predicate, params object[] values)

but changed source.Provider.CreateQuery() to source.Provider.CreateQuery<T>.

You will also have to do this for the static method OrderBy<T>.

这篇关于EF4 CTP5 - LINQ动态查询库抛出InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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