.Net Core 与数据库

时间:2024-03-14 11:06:13

查询

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;
}

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();


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);
}