将数据行返回为自定义类 [英] Return a datarow as a custom class
本文介绍了将数据行返回为自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有:
公共类rowEntry
{
public int idx;
public string descr;
public string param;
rowEntry(DataRow dr)
{
int.TryParse(dr [0] .ToString(),out idx);
descr = dr [1] .ToString();
param = dr [2] .ToString();
}
}
,作为IEnumerable的一部分,想要返回:
public rowEntry当前
...
return(rowEntry)tbl.Rows [posn];
...
但我无法将DataRow类型转换为rowEntry
我尝试过的事情:
我试过寻找答案但却找不到任何看起来很接近的答案足够
解决方案
所以,这至少可以编译:
public rowEntry当前
{
获得
{
尝试
{
rowEntry xre = new rowEntry(tbl.Rows [posn]);
返回xre;
}
catch(IndexOutOfRangeException)
{
抛出新的InvalidOperationException();
}
}
}
这是最好的方式吗?
I have:
public class rowEntry { public int idx; public string descr; public string param; rowEntry(DataRow dr) { int.TryParse(dr[0].ToString(), out idx); descr = dr[1].ToString(); param = dr[2].ToString(); } }
and, as part of IEnumerable, want to return:
public rowEntry Current ... return (rowEntry)tbl.Rows[posn]; ...
but I get cannot convert type DataRow to rowEntry
What I have tried:
I've tried searching for an answer but was unable to find anything that seemed close enough
解决方案
So, this does at least compile:
public rowEntry Current { get { try { rowEntry xre = new rowEntry(tbl.Rows[posn]); return xre; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } }
is it the best way?
这篇关于将数据行返回为自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文