在IEnumerable< DataRow>中转换数据类型.还是在Linq查询期间? [英] Converting Data Types in a IEnumerable<DataRow> or During Linq Query ?
问题描述
Hello All,
我在显示WPF DataGrid绑定内部具有DateTime类型的Datatable列时遇到问题.我正在尝试将datetime对象转换为字符串.它不想和我一起玩.这是我正在工作的Linq 语句,其中包含我无法工作的foreach语句,试图解析Type.
I'm having a issues displaying a Datatable column with the Type of DateTime Inside of a WPF DataGrid binding. I'm trying to do some conversion on the datetime object to a string. It doesnt want to play nice with me. Here below is my working Linq statement with my non working foreach statement attempting to parse the Type.
var dataTable = CaseLogs.CaseLogsDataTable.LogData;
IEnumerable<DataRow> list;
try
{
list = (from r in dataTable.AsEnumerable()
where r[columnName].ToString() == queryValue
select r);
foreach (DataRow dr in list)
{
for (int i = 0; i < 7; i++)
{
if (dr.ItemArray[i] is DateTime)
{
dr.ItemArray[i] = dr.ItemArray[i].ToString();
}
}
}
return list.CopyToDataTable<DataRow>();
}
catch (Exception ex)
{
Console.WriteLine("Error: GenericQuery " + ex);
}
return null;
推荐答案
rreeves84;
Hi rreeves84;
您声明,我正在尝试将datetime对象转换为字符串.它不想和我一起玩. ".您遇到错误了吗?如果是这样,那是什么错误?
You state, "I'm trying to do some conversion on the datetime object to a string. It doesnt want to play nice with me. ". Are you getting an error? If so what is the error?
我怀疑代码中的这一行出现了问题:
I suspect that the problem happens on this line in the code:
dr.ItemArray [i] = dr.ItemArray [i] .ToString();
dr.ItemArray[i] = dr.ItemArray[i].ToString();
当您尝试用String数据类型覆盖它时,dr.ItemArray [i]的数据类型是什么?
What is the data type of dr.ItemArray[i] when you try to over write it with a String data type?
这篇关于在IEnumerable< DataRow>中转换数据类型.还是在Linq查询期间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!