Linq To Sql的各种查询

一、Inner Join

  //request为查询条件
  var result = from a in db.TableA
 join b in db.TableB
 on a.ID equals b.ID
 where a.UserName == request.UserName || a.Mobile == request.Mobile
 select new ResponseInfo
 {
     UserName = ui.UserName,
     UserID = ui.ID,
     Mobile = ui.Mobile,
     Value = ue.Value
 };
 
二、Left  Join
//request为查询条件
var result = from si in db.TableA
     join ai in db.TableB on si.UserID equals ai.ID into ljTableB
     from ai in ljTableB.DefaultIfEmpty()
     join pi in db.TableC on si.ProductID equals pi.ID into ljTableC
     from pi in ljTableC.DefaultIfEmpty()
     join ps in db.TableD on si.SpecID equals ps.ID into ljTableD
     from ps in ljTableD.DefaultIfEmpty()
     select new InfoResposne
     {
         ID = si.ID,
         DisplayID = pi.DisplayID,
         ProductID = si.ProductID,
         ProductName = pi.ProductName,
         BarCode = pi.BarCode,
         SpecID = si.SpecID,
         Created = si.Created
     };
 
二、动态添加查询条件及翻页
//字符串
if (!string.IsNullOrWhiteSpace(request.DisplayID))
{
    result = result.Where(p => p.DisplayID == request.DisplayID);
}
//字符串包含
if (!string.IsNullOrWhiteSpace(request.UserName))
{
    userInfo = userInfo.Where(p => p.UserName.Contains(request.UserName));
}
//数字、状态类型
if (request.Amount!=null)
{
    result = result.Where(p => p.Amount == request.Amount);
}
//时间范围
  if (request.StockInStart != null)
{
    result = result.Where(p => p.Created >= request.StockInStart);
}
//时间范围
if (request.StockInEnd != null)
{
    result = result.Where(p => p.Created <= request.StockInEnd);
}
 
var response = new LogResponse();
response.TotalCount = result.Count();
response.DataList = result
            .OrderByDescending(p => p.Created)
            .Skip((request.PageIndex - 1) * request.PageSize)
            .Take(request.PageSize)
            .ToList();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值