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