- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在我的项目中创建了一个 DataRow:
DataRow datarow;
我想将此 DataRow 转换为任何类型的对象。我该怎么做?
最佳答案
这是我使用它的一种非常酷的方式。
public static T ToObject<T>(this DataRow dataRow)
where T : new()
{
T item = new T();
foreach (DataColumn column in dataRow.Table.Columns)
{
PropertyInfo property = GetProperty(typeof(T), column.ColumnName);
if (property != null && dataRow[column] != DBNull.Value && dataRow[column].ToString() != "NULL")
{
property.SetValue(item, ChangeType(dataRow[column], property.PropertyType), null);
}
}
return item;
}
private static PropertyInfo GetProperty(Type type, string attributeName)
{
PropertyInfo property = type.GetProperty(attributeName);
if (property != null)
{
return property;
}
return type.GetProperties()
.Where(p => p.IsDefined(typeof(DisplayAttribute), false) && p.GetCustomAttributes(typeof(DisplayAttribute), false).Cast<DisplayAttribute>().Single().Name == attributeName)
.FirstOrDefault();
}
public static object ChangeType(object value, Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
{
return null;
}
return Convert.ChangeType(value, Nullable.GetUnderlyingType(type));
}
return Convert.ChangeType(value, type);
}
关于c# - 如何将 DataRow 转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19673502/
我有一个扩展DataRow的类: import org.jdesktop.dataset.DataRow; public class MainDataRow extends DataRow { p
查看下面的代码块, DataTable _table = new DataTable(); //1) Why I need to Convert DataRow again into DataRow
我的代码: DataRow[] row = ByTotalTemplate.Select("TEMPLATE_ID=" + DisTemplateID); 一行包含TEMPLATE_ID、MIN_AM
使用这段代码: OracleDataTable dt = PlatypusSchedData.GetAvailablePlatypi(); OracleDataTable outputDt = new
假设我从多个线程访问一个DataTable。如果我想访问特定行,我怀疑我需要锁定该操作(我可能对此有误,但至少我知道这样我是安全的): // this is a strongly-typed tabl
我收到这个错误 "Error 8 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Coll
我有很多如下形式的代码: // assume that MyDataRow is a datarow from a typed dataset and has a property called So
在我的表中,有数据行注释,它存储为文本数据类型。 我有 Datarow idPersonalstamm,它存储为 int。 我的问题是,有时来自 Personalstamm 的数据会被复制到评论中。
我正在尝试将文本框中的新词添加到表格中: private void addAnswer_Click(object sender, EventArgs e) { // Get a new row
我的模块中有一个 cmdlet,它从数据库表中返回一组通用 DataRows,其名称由 -Name 指定。范围 # this returns a collection of datarows from
我在此代码行中得到一个空引用: **DataRow dr = tableSelectedItems.NewRow();** 我不知道为什么。谁能帮帮我吗?我的代码应该填写用户在数据列表中选择的书籍。
我尝试将值从自定义集合传递给 DataRow 类型变量,如下面的代码: GridHitInfo downHitInfo = null; DataRow row = gridView3.GetDataR
我正在尝试比较循环中的两个 DataRow。但是,以下 if 语句不会返回 true: if (dt1.Rows[0]["Name"] == dt2.Rows[b]["Name"]) { //
我在一个网站工作。我无法转换表格中行[""] 中的数值。我的代码是: public partial class bought : System.Web.UI.Page { protected
每当我在 MySql 中运行此查询时,它总是返回 3 行具有相同 ID,但记录不同的行,但在我的代码中,它只返回 1 行。 这是我的代码: public DataTable DBSelect(stri
我有以下问题。我将值作为 XML(如字符串)进行传递,并且不会为数字列传递默认值,例如 false 或 0。因此,我创建了一个字典,其中键是列名,值是传递的值。 这是我当前的代码 foreach (K
我正在尝试将 DataTable 中的所有 DateTime 值转换为字符串。这是我使用的方法: private static void ConvertDateTimesToStrings(DataT
我明白了: DataTable dtEntity = CreateDataTable(); drEntity = dtEntity.NewRow(); 然后我将数据添加到该行(或不添加)。一大堆代
我遇到了 DataRow 的问题,这让我很头疼。 使用 OleDbConnection 从 Excel 电子表格中读取数据行。 如果我尝试使用列名从 DataRow 中选择数据,即使那里有数据,它也会
我正在尝试编写一个单元测试来比较两个数组。我已经定义了单元测试: [DataTestMethod] [DataRow( new[] { "COM3", "COM1", "COM2" },
我是一名优秀的程序员,十分优秀!