zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    pychram 2018-01 安装pyQT5报错
    pyqt 8行内就可以跑一个浏览器
    sql server无log ldf日志文件附件mdf数据库重新生成ldf日志文件
    解决Specifying a namespace in include()withou providing an app_name
    Java 连接池的工作原理(转)
    使用from __future__ import unicode_literals时要注意的问题
    详细介绍Redis的几种数据结构以及使用注意事项(转)
    再谈Redis应用场景(转)
    Redis的LRU机制(转)
    深入理解Redis主键失效原理及实现机制(转)
  • 原文地址:https://www.cnblogs.com/gossip/p/4832046.html
Copyright © 2011-2022 走看看