我试图将通用集合(List)转换为DataTable.我找到了以下代码来帮助我这样做:
// Sorry about indentation
public class CollectionHelper
{
private CollectionHelper()
{
}
// this is the method I have been using
public static DataTable ConvertTo<T>(IList<T> list)
{
DataTable table = CreateTable<T>();
Type entityType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
foreach (T item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
table.Rows.Add(row);
}
return table;
}
public static DataTable CreateTable<T>()
{
Type entityType = typeof(T);
DataTable table = new DataTable(entityType.Name);
PropertyDescriptorCollection …
Run Code Online (Sandbox Code Playgroud) 我需要在内存中使用基于来自GridView的列和方向的DataTable.该函数需要如下所示:
public static DataTable resort(DataTable dt, string colName, string direction)
{
DataTable dtOut = null;
....
}
Run Code Online (Sandbox Code Playgroud)
我需要帮助填写这个功能.我想我可以使用Select语句,但我不知道如何.由于此浏览器,我无法点击评论,但您可以向我展示一个就地或新的DataTable解决方案.对于那些向我显示指针的人来说,我需要一个类似于原型的编码功能.
怎么样:
// ds.Tables[0].DefaultView.Sort="au_fname DESC";
public static void Resort(ref DataTable dt, string colName, string direction)
{
string sortExpression = string.Format("{0} {1}", colName, direction);
dt.DefaultView.Sort = sortExpression;
}
Run Code Online (Sandbox Code Playgroud) 如何DataColumn
向DataTable
已包含数据的对象添加新内容?
伪代码
//call SQL helper class to get initial data
DataTable dt = sql.ExecuteDataTable("sp_MyProc");
dt.Columns.Add("NewColumn", type(System.Int32));
foreach(DataRow row in dr.Rows)
{
//need to set value to NewColumn column
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在DataTable的Columns属性上执行简单的LINQ查询:
from c in myDataTable.Columns.AsQueryable()
select c.ColumnName
Run Code Online (Sandbox Code Playgroud)
但是,我得到的是:
无法找到源类型"System.Linq.IQueryable"的查询模式的实现.找不到"选择".考虑明确指定范围变量'c'的类型.
如何让DataColumnCollection与LINQ一起玩得很好?
我使用DataTable和有关用户的信息,我希望在此DataTable中搜索用户或用户列表.我试试但是不起作用:(
这是我的c#代码:
public DataTable GetEntriesBySearch(string username,string location,DataTable table)
{
list = null;
list = table;
string expression;
string sortOrder;
expression = "Nachname = 'test'";
sortOrder = "nachname DESC";
DataRow[] rows = list.Select(expression, sortOrder);
list = null; // for testing
list = new DataTable(); // for testing
foreach (DataRow row in rows)
{
list.ImportRow(row);
}
return list;
}
Run Code Online (Sandbox Code Playgroud) 我想用EPPlus将数据表导出到excel文件,数据表具有int类型的属性,所以我想在excel文件中,格式相同.有谁知道用这些方法将DataTable导出到Excel?
我有一个DataTable有5列和10行.现在我想向DataTable添加一个新列,我想将DropDownList值分配给New Column.所以DropDownList值应该添加10次到New Column.这该怎么做?注意:不使用FOR LOOP.
例如:我的现有DataTable是这样的.
ID Value
----- -------
1 100
2 150
Run Code Online (Sandbox Code Playgroud)
现在我想在此DataTable中添加一个新列"CourseID".我有一个DropDownList.它的选择值是1.所以我现有的表应如下所示:
ID Value CourseID
----- ------ ----------
1 100 1
2 150 1
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
有没有一种很好的方法将IEnumerable转换为DataTable?
我可以使用反射来获取属性和值,但这似乎有点低效,是否有内置的东西?
(我知道这些例子:ObtainDataTableFromIEnumerable)
编辑:
这个问题通知我处理空值的问题.
我在下面写的代码正确处理空值.
public static DataTable ToDataTable<T>(this IEnumerable<T> items) {
// Create the result table, and gather all properties of a T
DataTable table = new DataTable(typeof(T).Name);
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Add the properties as columns to the datatable
foreach (var prop in props) {
Type propType = prop.PropertyType;
// Is it a nullable type? Get the underlying type
if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
propType = new NullableConverter(propType).UnderlyingType;
table.Columns.Add(prop.Name, propType);
}
// …
Run Code Online (Sandbox Code Playgroud) 我有一个DataTable有5列:
DataTable包含5行.
如何将标签控件中的金额列的总和显示为"总金额"?
我有一个包含多列的DataTable.我想List<String>
从DataTable的第一列中获取一个.我怎样才能做到这一点?