2011-11-10 1 views
1

IQueryable을 사용하여 특정 레코드를 검색하려고합니다. 하지만 오류가 발생합니다 '일반 메서드 없음'형식의 'System.Linq.Queryable'은 제공된 형식 인수 및 인수와 호환됩니다. 메소드가 generic이 아닌 경우 형식 인수가 제공되지 않아야합니다. '. 선택한 행 ID가 있는데 표시 할 수 없습니다. 여기 내 코드가있다.'System.Linq.Queryable'형식의 '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을 입력 할 때 예외를 throw합니다. 내 코드는

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

typeof(Quarable)의 사용. 모든 타입이 맞습니까? – 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) 대신

관련 문제