当前位置:   article > 正文

.Net Core 与数据库

.Net Core 与数据库

查询

Linq

var indexList = new long[] { 1, 2, 3};
List<long> list = new List<long>();
if (String.IsNullOrWhiteSpace(request.Key) == false)
{
    var ret = from aa in _db.TblAA
              join bb in _db.TblBB
              on aa.PId equals bb.Id
              join cc in _db.TblCC
              on aa.CId equals cc.Id
              join dd in _db.TblDD
              on bb.Id equals dd.DId
              where ((cc.Content.IndexOf(request.Key) != -1) && (indexList.Contains(cc.CTId)))
              select bb.Id;
    if (ret.Count() > 0)
    {
        foreach (var item in ret)
        {
            list.Add(item);
        }
    }
}
if (list.Count() > 0)
{
    string retStr = string.Join(",", list.Distinct());
    resp.IdListStr = retStr;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

Raw SQL

 string sql = "select A.* from tbl_AA A left join tbl_AA_BB_assoc B on A.id = B.Bid where ";
 if (string.IsNullOrWhiteSpace(req.Name))
 {
     sql += "(name like '%%' or name is NULL) ";
 }
 else
 {
     sql += "(name like '%" + req.Name + "%')";
 }

 var AA = _db.Tbl_AA.FromSqlRaw(sql).ToList();


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
List<TblP> tblPList = new List<TblP>();
var PList2 = _db.TblPs.Where(p=>p.A == request.A && p.B == request.B).ToList();

if ((request.DCCId!=null) && (request.DCCId > 0))
{
    PList2 = PList2.Where(p => p.DCCId == request.DCCId).ToList();
} 
if (PList2.Count > 0)
{
    tblPList.AddRange(PList2);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/258875
推荐阅读
相关标签
  

闽ICP备14008679号