我可以在实体框架分页语法中使用join吗?

时间:2019-04-08 11:25:45

标签: c# asp.net-mvc entity-framework pagination

我想知道是否有人可以在这里提供帮助。我是Entity Framwork的新手。我有2个不同的Query.I想要加入它们并获得1.这是我的代码:

 public static List<BankDepositHistory> GetAllByPagination(int page ,int stepes)
        {

            page=page-1;

            using(MyketAdsEntities context = new MyketAdsEntities())
            {
                var transactionlist = context.BankDepositHistories.ToList();
                var start = page * stepes;
              var result=  context.BankDepositHistories.OrderByDescending(c=>c.AccountId)

                    //anny code that give me count as field
                    .Skip(start)
                    .Take(stepes)
                    .ToList();
                return (result);

            }
        }
        public static int GetCount()
        {
            using (MyketAdsEntities context = new MyketAdsEntities())
            {
                int count = context.BankDepositHistories.Count();
                return count;
            }

        }

如您所见,我有2种方法。我只想拥有GetAllByPagination。 非常感谢

1 个答案:

答案 0 :(得分:1)

假设您在Entity下面,那么下面的设置应该可以使用。

public BankDepositHistory
{

public string UserName {get;set}

//etc..

}

创建视图模型

public class BankDepositHistoryVM
{

public List<BankDepositHistory> bankDetails {get;set;}

public int Count {get;set;}

}

返回视图模型

 public static List<BankDepositHistoryVM> GetAllByPagination(int page ,int stepes)
    {

        page=page-1;

        using(MyketAdsEntities context = new MyketAdsEntities())
        {
            var transactionlist = context.BankDepositHistories.ToList();
            var start = page * stepes;
          var result=  context.BankDepositHistories.OrderByDescending(c=>c.AccountId)
                .Skip(start)
                .Take(stepes)
                .ToList();

  List<BankDepositHistoryVM> resultVM = new List<BankDepositHistoryVM>();

  resultVM.bankDetails = result;
  resultVM.Count = result.Count();

            return resultVM;

        }
    }

调用方法:

List<BankDepositHistory> bankDetails = className.GetAllByPagination.bankDetails;

int count = className.GetAllByPagination.Count;

List<BankDepositHistoryVM> allDetails = className.GetAllByPagination();

希望有帮助。