我第一次尝试实现通用存储库模式和框架单元。我没有在手头的项目上使用 MVC。 请看一下 Generic Repository 类中包含的这个方法:
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
它一定是一个强大的方法,并且很好地实现了 DRY 的目标。 我的问题是,我不能将结果排序为降序?谁能写几行代码来帮助我解决这个问题?谢谢,
最佳答案
看看这个:http://prodinner.codeplex.com/还有这个http://efmvc.codeplex.com/ . 这些项目是简单架构的很好示例,您可以了解通用存储库的实现方式和使用方式。
关于c# - 首先使用实体框架代码实现通用存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16611031/