0
在我的解決方案,創建通用庫OrderBy&OrderByDescending DateTime在通用存儲庫?
public abstract class GenericRepository<C,T> : Repo.IGenericRepository<T>
where T : class
where C : DbContext, new(){
private C _entities = new C();
public C Context
{
get { return _entities; }
set { _entities = value; }
}
public virtual IQueryable<T> GetAll(
Expression<Func<T,object>> predicate,
Sorted _sort = Sorted.ASC,
int _max = 0,
int _skip = 0
) {
IQueryable<T> query = _sort == Sorted.ASC ? //Sorted is enum
_entities.Set<T>().OrderBy(predicate).Skip(_skip).Take(_max) :
_entities.Set<T>().OrderByDescending(predicate).Skip(_skip).Take(_max);
return query;
}
}
,並呼籲
var db = new JobRepository();
db.GetAll(e=>e.jcudate, sort, a, b).ToList();
錯誤:
無法轉換類型「System.Nullable`1 [System.DateTime的, mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]'鍵入'System.Object'。 LINQ to Entities僅支持投射EDM基元或枚舉類型。
但是,如果使用
WSCEntities dbs = new WSCEntities();
dbs.jobservs.OrderBy(e=>e.jcudate).Skip(b).Take(a).ToList();
一點問題都沒有..,請建議我應該怎麼解決這個問題?
感謝@JenishRabadiya的建議。並將IQueryable替換爲IEnumerable – 2015-02-12 07:39:33