2012-04-27 6 views
1

私は(あなたがその全体hereでそれを見ることができます)このクラスを持っている...Entity Frameworkのとキャスト<>

internal class BaseRepository<I, C> : IRepository<I> 
    where I : class, IBaseObject 
    where C : BaseObject 
{ 
    private Context _context; 

    public IEnumerable<I> FindBy(Expression<Func<I, bool>> predicate) 
    { 
     return _context.Set<C>().ToList().Cast<I>().AsQueryable().Where(predicate); 
    } 

    // other methods. 
} 

私はこれを動作することができますどのように私は、私は信じている.ToList()を呼び出す必要はありません。 EFは.ToList().AsQueryable()せずにこれを使用して.Set<C>()

のすべてを返すようになり、エラーが発生します。

System.NotSupportedException:タイプをキャストすることができません「Sln.DAL.Sql.Entities.Project」T o「Sln.DAL.Entities.IProject」と入力します。エンティティへのLINQは、EDMプリミティブまたは列挙型のキャストのみをサポートします。

+0

マインドを使用することはでき

internal class BaseRepository<I, C> : IRepository<I> where I : class, IBaseObject where C : BaseObject, I 

制約を追加した場合は?それを削除するとどうなりますか? –

+0

申し訳ありません - 追加されました。 – Brendan

+0

BaseObjectは常にIから派生していますか? – Phil

答えて

0

私たちは、あなたが `ToList`を必要とする理由を告げ

_context.Set<C>().Where(predicate).AsEnumerable(); 
+0

'context.Set ().Where(述語).AsEnumerable();は、linq2EFで例外をスローします。 –

関連する問題