Linq not in 查询 时间字符串string转datetime类型查询

1、SQL查询原型:

select * from SymptomPic where PicPath not in ('图片路径1','ssssss','sdsd','sdsd') and SymptomID = 12

Linq语句:

from t in db.SymptomPic where !new string[] { "aaa", "bbb", "ccc" }.Contains(t.PicPath) && t.SymptomID == 12
select new 
{
  t.ID,
  t.SymptomID,
  t.PicPath,
  t.State,
  hourLength = ((DbFunctions)null).DateDiffHour(t.created_time.Value, t.created_time.Value),
  less = ((DbFunctions)null).DateDiffHour(t.created_time.Value, t.created_time.Value) < 6 ? "是" : "否",
  middle = ((DbFunctions)null).DateDiffHour(t.created_time.Value, t.created_time.Value) >= 6 
  && ((DbFunctions)null).DateDiffHour(t.created_time.Value, t.created_time.Value) <=24 ? "是" : "否",
  more = ((DbFunctions)null).DateDiffHour(t.created_time.Value, t.created_time.Value) > 24 ? "是" : "否"
};
}

实战项目示例代码:

var QUERY = from t in dbContent.SymptomPicService
            where !new string[] { "aaa", "bbb", "ccc" }.Contains(t.PicPath) && t.SymptomID == 12
            select new
            {
                t.ID,
                t.SymptomID,
                t.PicPath,
                t.State
            };

2、SQL删除原型

Delete from SymptomPic where PicPath not in ('图片路径1','ssssss','sdsd','sdsd') and SymptomID = 12

Linq语句删除:

var querySymptomPic = from t in db.SymptomPic where !new string[] { "aaa", "bbb", "ccc" }.Contains(t.PicPath) && t.SymptomID == 12 select t;
foreach (var del in querySymptomPic)
{
  db.SymptomPic.DeleteOnSubmit(del);
}
db.SubmitChanges();

3、Linq 时间字符串string转datetime类型查询

result = result.Where(x => (DateTime)(object)x.upString >= Convert.ToDateTime(request.beginTime));

实战项目示例代码:

Dictionary<string, string> dicList = new Dictionary<string, string>();

List<string> list = new List<string>();
foreach (var item in dicList)
{
    list.Add(item.Value);
}

string[] strArray = list.ToArray();

var querySymptomPic = from t in dbContent.SymptomPicService where !strArray.Contains(t.PicPath) && t.SymptomID == id select t;


foreach (var del in querySymptomPic)
{
    dbContent.SymptomPicService.Remove(del);
}
dbContent.SaveChanges();

1、Controller控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Medicine.BizLogic;
using Medicine.Common;
using Medicine.Entity;
using System.Configuration;
using Newtonsoft.Json;

namespace Medicine.Api.Controllers
{
    public class SymptomController : Controller
    {
        [HttpPost]
        public ActionResult UpdateSymptom(SymptomViewModel data)
        {
            try
            {
                //上传文件
                string path = string.Empty;
                Dictionary<string, string> dictNew = new Dictionary<string, string>();
                Dictionary<string, string> dictOld = new Dictionary<string, string>();

                if (!string.IsNullOrEmpty(data.PicString))
                {
                    string[] strArray = data.PicString.Split(',');
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        string value = strArray[i].ToString();
                        ///判断是否有旧图片
                        if (StringPlus.SubstringChar(value.ToLower(), 4) == "http")
                        {
                            dictOld.Add("id" + i.ToString(), StringPlus.SubstringIndexOfChar(value, "/upload"));
                        }
                        else
                        {
                            dictNew.Add("pic" + i.ToString(), value);
                        }
                    }
                }

                var model = new Symptom()
                {
                    ID = data.ID,
                    UserID = data.UserID,
                    Title = data.Title,
                    ContentDescribe = data.ContentDescribe,
                };
                bool result = biz.UpdateSymptomNew(model, dictNew, dictOld);
                return ResultMessage.Tool.BoolResult(result);
            }
            catch (Exception ex)
            {
                Log4netHelper.Tool.LogError(address + "UpdateSymptom()", ex.Message);
                return ResultMessage.Tool.ErrorResult(ex);
            }
        }
    }
}

2、Service数据库访问层

using Medicine.Common;
using Medicine.Entity;
using Medicine.IService;
using Medicine.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Medicine.Service
{
    public class SymptomPicService : BaseEntities, ISymptomPicService
    {
        public bool DeleteByDictionary(long id, Dictionary<string, string> dicList)
        {
            try
            {
                if (dicList.Count > 0)
                {
                    List<string> list = new List<string>();
                    foreach (var item in dicList)
                    {
                        list.Add(item.Value);
                    }
                    string[] strArray = list.ToArray();

                    var query = from t in dbContent.SymptomPicService where !strArray.Contains(t.PicPath) && t.SymptomID == id select t;

                    foreach (var item in query)
                    {
                        dbContent.SymptomPicService.Remove(item);
                    }
                    dbContent.SaveChanges();
                }
                else
                {
                    Delete(id);
                }
                return true;
            }
            catch (Exception ex)
            {
                Log4netHelper.Tool.LogError(address + "Delete()", ex.Message);
                return false;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值