【发布时间】:2023-04-03 03:57:01
【问题描述】:
我有这个代码:
public bool GetHolidays(DataSet ds,List<string> dates,DataTable dt)
{
for (int i = 0; i <= dates.Count; i++)
{
bool weekOff = ds.Tables[0].AsEnumerable().Where(x=>(x.Field<string>("WeekOffDays")==dates[i]).ToString()).Any(); // error in this line
if(weekOf)
return true;
return false;
}
}
我收到错误消息:
无法将“System.DateTime”类型的对象转换为“System.String”类型
为了克服这个问题,我尝试了:
DateTime dateTime;
bool weekOff = ds.Tables[0].AsEnumerable().Where(x=>DateTime.TryParse((x.Field<string>
("WeekOffDays")==dates[i]).ToString(),out dateTime)).Any();
但错误仍然存在。
我尝试使用 DateTime,但是当我写它时,它给了我另一个错误提示
运算符 == 不能用于 DateTime 和字符串
请注意: WeekOffDays 列的类型为 datetime
如何解决?
【问题讨论】:
-
尝试在第二个查询中将其更改为
x.Field<DateTime> -
我试过了,但它给了我这个错误:Operator == cannot be used for DateTime and string
-
是的,试试@HienNguyen 回答
-
WeekOffDays类型是 varchar 还是 dateTime2? -
所以,尝试
ds.Tables[0].AsEnumerable().Any(x => (x.Field<DateTime>("WeekOffDays")).ToString("dd/MM/yyyy") == dates[i])bu 使用ToString()的正确格式