2011-11-10 13 views
1

IQueryableを使用して特定のレコードを取得します。しかし、私はエラー '一般的なメソッドはありません'タイプ 'System.Linq.Queryable'の型は、指定された型引数と引数と互換性があります。メソッドが非ジェネリックである場合、タイプ引数は提供されません。 '選択した行IDを取得しましたが、表示できません。ここに私のコードです。一般的なメソッドがありません 'タイプ'の 'Where'は、指定された型引数および引数と互換性があります。

internal static IQueryable GetRecordsFromPrimaryKeys(this IQueryable datasource, List<FilterDescriptor> primaryKeys) 
    { 
     IQueryable data = datasource; 

     ParameterExpression paramExp = null; 
     bool firstLoop = false; 
     System.Linq.Expressions.Expression predicate = null; 

     var RecordType = datasource.GetObjectType(); 
     paramExp = RecordType.Parameter(); 

     foreach (FilterDescriptor primaryKey in primaryKeys) 
     { 
      if (!(firstLoop)) 
      { 
       predicate = data.Predicate(paramExp, primaryKey.ColumnName, primaryKey.Value, FilterType.Equals, false, RecordType); 
       firstLoop = true; 
      } 
      else 
      { 
       predicate = predicate.AndPredicate(data.Predicate(paramExp, primaryKey.ColumnName, primaryKey.Value, FilterType.Equals, false, RecordType)); 
      } 
     } 

     if (paramExp != null && predicate != null) 
     { 
      var lambda = Expression.Lambda(predicate, paramExp); 
      data = data.Provider.CreateQuery(
         Expression.Call(
         typeof(Queryable), 
         "Where", 
         new Type[] { data.ElementType }, 
         data.Expression, 
         lambda 
         ) 
        ); 

     } 

     return data; 
    } 

私のコードは、IEnumerableを/のIQueryable/ICollectionを適しています。しかし、私はクラスをキーワードvirtualで指定し、ICollectionとしてタイプすると例外がスローされます。私のコードは

public class RoomType 
{ 
    public int ID { get; set; } 

    [MaxLength(10, ErrorMessage = "Room code cannot be longer than 10 characters.")] 
    public string Code { get; set; } 

    [MaxLength(50, ErrorMessage = "Room name cannot be longer than 50 characters.")] 
    public string Name { get; set; } 

    public virtual ICollection<RoomCategory> RoomCategories { get; set; } 
} 

となります。ランダムな値は、キーワード 'virtual'を使用している間に 'RecordType'に追加されます。私はこれが例外につながると思う。ソリューションを探しています。

私は何がうまくいかないのか分かりません。どんな提案も大歓迎です。

ありがとうございました。

+0

私はあなたのコードは動作するはずだと思うを使用してみてください。あなたのタイプはすべて一致しますか? – svick

答えて

1

私は同様の状況に遭遇しました。この問題は、実際のエンティティではなく「プロキシ」を扱う場合があるという事実に由来します。したがって、RecordTypedata.ElementTypeと一致するようにしたいとします。

試してみてください。

var recordType = datasource.GetObjectType(); 

// make sure we have the correct type (not the proxy) 
if (recordType.BaseType.Name != "Object") 
    recordType = recordType.BaseType; 

いっそ、試してみてください。

var recordType = data.ElementType 
+0

ねえ...。それは魅力のように働いた:)。どうもありがとう!。 – RGR

0

typeof(Enumerable)代わりのtypeof(Quarable)

関連する問題