C# Lambda 实体框架:有什么方法可以使用谓词 Lambda 按行号选择记录?
我想在存储库之外进行分页,但存储库返回 IList。由于我不想不必要地提取数据,并且不想在我的存储库中创建特定的分页方法,有没有办法在Where扩展方法中按页过滤记录?
我想执行类似的操作:
var myRecords = ProductRepo.Get( p => p.Name.StartsWith("Pepsi") &&
p.Row_Number() > 9 &&
p.Row_Number() < 21);
获取存储库代码(当然不处理行号):
public IList<Product> Get( Expression<Func<Product, bool>> filter = null)
{
if( filter == null)
return _dc.Products.ToList<Product>();
else
return _dc.Products.Where(filter).ToList<Product>();
}
I want to do paging outside of my Repository, but the Repository returns IList. Since I don't want to pull data unnecessarily, and don't want to create specific Paging methods in my Repositories, Is there any way to filter records by page in the Where extension method?
I want to do something like:
var myRecords = ProductRepo.Get( p => p.Name.StartsWith("Pepsi") &&
p.Row_Number() > 9 &&
p.Row_Number() < 21);
The Get repository code (doesn't handle row number of course):
public IList<Product> Get( Expression<Func<Product, bool>> filter = null)
{
if( filter == null)
return _dc.Products.ToList<Product>();
else
return _dc.Products.Where(filter).ToList<Product>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的存储库返回IQueryable(不要调用.ToList())。然后您将可以使用跳过和采取。
EF 不支持 ROWNUM。
http://social. msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/055911ff-6db6-48cb-8e50-bd6415b7eefe/
Return IQueryable from your Repository (don't call .ToList()). You will be able to use Skip and Take then.
ROWNUM is not supported in EF.
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/055911ff-6db6-48cb-8e50-bd6415b7eefe/
就这样吗?
Like that?