.NET经销商实战(五)——linq分页查询
Linq分页查询
这个是为了方便我们查询时直接传当前页,和当前页码,方便利于我们做分页
Repository代码如下:
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using DealerPlatform.Domain.Models;
using Microsoft.EntityFrameworkCore;
namespace DealerPlatform.Core.Repository
{
public class Repository : IRepository where TEntity : BaseEntity
{
public Repository(DealerPlatformContext context)
{
Context = context;
}
public DealerPlatformContext Context { get; }
///
/// 获取集合
///
///
public List GetList()
{
return Context.Set().ToList();
}
public async Task> GetListAsync()
{
var dbset = Context.Set();
return await dbset.ToListAsync();
}
///
/// 分页
///
///
///
public async Task> GetListAsync(int pageIndex, int pageSize, string sort)
{
int skipNum = (pageIndex - 1) * pageSize;
var dbset = Context.Set();
return await dbset.OrderBy(m => sort).Skip(skipNum).Take(pageSize).ToListAsync();
// return await dbset.WhereAsync(predicate).ToListAsync();
}
//对集合进行筛选
///
/// 对集合进行筛选 IEnumerable
///
///
///
public List GetList(Func predicate)
{
var dbset = Context.Set();
return dbset.Where(predicate).ToList();
}
public async Task> GetListAsync(Expression> predicate)
{
var dbset = Context.Set();
return await dbset.Where(predicate).ToListAsync();
// return await dbset.WhereAsync(predicate).ToListAsync();
}
public async Task> GetListAsync(int pageIndex, int pageSize, string sort, Expression> predicate)
{
int skipNum = (pageIndex - 1) * pageSize;
var dbset = Context.Set();
return await dbset.OrderBy(m => sort).Skip(skipNum).Take(pageSize).Where(predicate).ToListAsync();
// return await dbset.WhereAsync(predicate).ToListAsync();
}
///
/// 通过id获取当前实体信息
///
///
public TEntity GetEntityById(int id)
{
var data = Context.Set().Where(s => s.Id == id).FirstOrDefault();
return data;
}
public TEntity Get(Func predicate)
{
var dbset = Context.Set();
return dbset.Where(predicate).FirstOrDefault();
}
public async Task GetAsync(Func predicate)
{
var dbset = Context.Set();
return dbset.Where(predicate).FirstOrDefault();
// return await dbset.WhereAsync(predicate).ToListAsync();
}
///
/// 添加方法
///
///
///
public TEntity Add(TEntity entity)
{
var dbset = Context.Set();
var res = dbset.Add(entity).Entity;
Context.SaveChanges();
return res;
}
public async Task AddAsync(TEntity entity)
{
var dbset = Context.Set();
var res = (await dbset.AddAsync(entity)).Entity;
Context.SaveChanges();
return res;
}
///
/// 删除方法
///
///
///
public TEntity Delete(TEntity entity)
{
var dbset = Context.Set();
var res = dbset.Remove(entity).Entity;
Context.SaveChanges();
return res;
}
///
/// 修改方法
///
///
///
public TEntity Update(TEntity entity)
{
var dbset = Context.Set();
var oldData = dbset.Where(s => s.Id == entity.Id).FirstOrDefault();
if (oldData != null)
{
var updateData = dbset.Update(entity).Entity;
Context.SaveChanges();
return updateData;
}
else
{
throw new Exception("当前数据异常!");
}
}
public async Task UpdateAsync(TEntity entity)
{
var dbset = Context.Set();
var oldData = dbset.Where(s => s.Id == entity.Id).FirstOrDefault();
if (oldData != null)
{
var updateData = dbset.Update(entity).Entity;
Context.SaveChanges();
return updateData;
}
else
{
throw new Exception("当前数据异常!");
}
}
}
}
IRepository代码如下:
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using DealerPlatform.Domain.Models;
namespace DealerPlatform.Core.Repository
{
public interface IRepository
{
}
public interface IRepository : IRepository where TEntity : BaseEntity
{
DealerPlatformContext Context { get; }
TEntity Add(TEntity entity);
Task AddAsync(TEntity entity);
TEntity Delete(TEntity entity);
TEntity Get(Func predicate);
Task GetAsync(Func predicate);
TEntity GetEntityById(int id);
List GetList();
List GetList(Func predicate);
Task> GetListAsync();
Task> GetListAsync(int pageIndex, int pageSize, string sort);
Task> GetListAsync(Expression> predicate);
Task> GetListAsync(int pageIndex, int pageSize, string sort, Expression> predicate);
TEntity Update(TEntity entity);
Task UpdateAsync(TEntity entity);
}
}
service项目ProductApp文件夹的ProductService类代码如下:
代码修改了一些,为了传分页需要的参数
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using DealerPlatform.Core.Repository;
using DealerPlatform.Domain.Models;
using DealerPlatform.Service.ProductApp.Dto;
namespace DealerPlatform.Service.ProductApp
{
public partial class ProductService : IProductService
{
public ProductService(
IRepository productRepo,
IRepository productSaleRepo,
IRepository productSaleAreaDiffRepo,
IRepository productPhotoRepo,
IMapper mapper)
{
ProductRepo = productRepo;
ProductSaleRepo = productSaleRepo;
ProductSaleAreaDiffRepo = productSaleAreaDiffRepo;
ProductPhotoRepo = productPhotoRepo;
Mapper = mapper;
}
public IRepository ProductRepo { get; }
public IRepository ProductSaleRepo { get; }
public IRepository ProductSaleAreaDiffRepo { get; }
public IRepository ProductPhotoRepo { get; }
public IMapper Mapper { get; }
///
/// 获取商品数据
///
///
public async Task> GetProductDto(string sort = null, int pageIndex = 1, int pageSize = 30)
{
sort ??= "ProductName";
// int skipNum = (pageIndex - 1) * pageSize;
// var products = (from p in (await ProductRepo.GetListAsync())
// orderby p.GetType().GetProperty(sort).GetValue(p)
// select p).Skip(skipNum).Take(pageSize).ToList();
//获取商品主档信息
var products = await ProductRepo.GetListAsync(pageIndex, pageSize, sort);
var dtos = Mapper.Map>(products);
var productPhotos = await GetProductPhotosByProductNo(dtos.Select(s => s.ProductNo).ToArray());
var productSales = await GetProductSalesByProductNo(dtos.Select(s => s.ProductNo).ToArray());
dtos.ForEach(s =>
{
s.ProductPhoto = productPhotos.FirstOrDefault(c => c.ProductNo == s.ProductNo);
s.ProductSale = productSales.FirstOrDefault(c => c.ProductNo == s.ProductNo);
});
//根据productId取到属性
return dtos;
}
}
}
controller中的ProductController控制器代码如下:
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DealerPlatform.Service.ProductApp;
using DealerPlatform.Service.ProductApp.Dto;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DealerPlatform.Web.Controllers
{
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ProductController : BaseController
{
public ProductController(IProductService productService)
{
ProductService = productService;
}
public IProductService ProductService { get; }
[HttpGet]
public async Task> GetProductDtosAsync(string? sort, int pageIndex = 1, int pageSize = 30)
{
var data = await ProductService.GetProductDto(sort, pageIndex, pageSize);
return data;
}
}
}
以上就是linq分页查询的修改