【问题标题】:Entity Framework SqlQuery : how to do implement .where .orderby extend and include navigation properties?实体框架 SqlQuery:如何实现 .where .orderby 扩展并包含导航属性?
【发布时间】:2019-10-17 13:49:38
【问题描述】:

Entity Framework 6 中如何对这个动态查询进行分页处理?

BillDetailLogsDto是自定义结果DTO不是数据库实体,如何sqlquery包含导航属性和不使用SqlParameter无法传递参数如何实现sqlquery使用.Where.OrderBy扩展

using (ProjectDbContext context = new ProjectDbContext())
{
    var query = context.Database.SqlQuery<BillDetailLogsDto>("SELECT *, SUM(Amount) OVER (ORDER BY Id) AS QSAmount FROM BillDetailLogs").AsQueryable();
    var result = query.Select(t => new
            {
                CustomerName = t.Customer.Name,
                t.CustomerId,
                t.Type,
                t.SortId,
                t.Amount,
                QAmount=t.QSAmount,
                t.ObjDate,
                t.OpRemark,
                t.Remark,
                t.SendOrderId,
                CreatedUserName = t.CreatedUser.RealName,
                t.CreatedDate,
            }).OrderByDescending(t => t.SortId);

    return Json(result, JsonRequestBehavior.AllowGet);
}

【问题讨论】:

  • ToPagedList 需要 pageNumber 和 currentPage 作为参数:public static IPagedList&lt;T&gt; ToPagedList&lt;T&gt;(this IQueryable&lt;T&gt; superset, int pageNumber, int pageSize); 对于导航属性,您可能需要使用 Include 方法,例如billDetailLogsDto.Include(x =&gt; x.Customer).Include("MoreStuff").Where(...)
  • @beiduoan 你搞定了吗?

标签: c# .net entity-framework-6


【解决方案1】:

只需将以下内容添加到您的查询中:

.Skip(pageSize * page).Take(pageSize)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多