前因
使用linq语句在数据库中查询一段时间内的信息。将表中的数据给实体,尴尬的是表中date这个字段是string类型,需要转换。
代码
listStartTime = (from u in dbcontext.t_user
join a in dbcontext.t_addmcoinrecord on u.userID equals a.userID
join g in dbcontext.t_department on u.departmentID equals g.departmentID
where (start <= Convert.ToDateTime(a.date) && Convert.ToDateTime(a.date) <= end)
where (g.departmentName == userGrade)
select new stageModel()
{
pai = 0,
dayMcoin = a.addMcoin,
userID = a.userID,
userName = u.userName,
userGrade = g.departmentName,
startTime = startTime,
endTime = endTime,
StageConfluence = "",
}).ToList();
运行走起——>报错:不识别方法
那这怎么转换呢?
最好的方式就是不在linq与里边转化哈,先按着其他条件,没有时间的条件查出来信息给实体。然后再对实体的字段转换类型进行时间的筛选。
代码
listStartTime = (from u in dbcontext.t_user
join a in dbcontext.t_addmcoinrecord on u.userID equals a.userID
join g in dbcontext.t_department on u.departmentID equals g.departmentID
where (g.departmentName == userGrade)
select new stageModel()
{
pai = 0,
dayMcoin = a.addMcoin,
userID = a.userID,
userName = u.userName,
userGrade = g.departmentName,
startTime = startTime,
endTime = endTime,
date =a.date ,
StageConfluence = "",
}).ToList();
for (int i = 0,j=0; i < listStartTime.Count; i++)
{
if (start <= Convert.ToDateTime( listStartTime[i].date) && Convert.ToDateTime( listStartTime[i].date) <= end )
{
listTest.Add(listStartTime[i]);
listTest[j].Mcoin = Convert.ToDecimal(listTest[j].dayMcoin);
j++;
}
}
说点啥:遇到问题没有很快的想出解决方案,还是我的套路还是没有用好。套路要用到问题还没有出来我们就会知道解决方案了。。。