C# 没有CopyToDatatable()方法
我有一个linq到dataTable的查询,如下所示:C# 没有CopyToDatatable()方法,c#,C#,我有一个linq到dataTable的查询,如下所示: `var ShowResult = from r in Result.AsEnumerable() where Convert.ToInt32(r.Field<double>("ASLVAM") / r.Field<double>("GEST")) > 60 orderby Convert.ToI
`var ShowResult = from r in Result.AsEnumerable()
where Convert.ToInt32(r.Field<double>("ASLVAM") / r.Field<double>("GEST")) > 60
orderby Convert.ToInt32(r.Field<double>("ASLVAM") / r.Field<double>("GEST")) descending
select new
{
pascode = r.Field<string>("PAS_CODE"),
melli = r.Field<string>("CODEMELI"),
name = r.Field<string>("NAM"),
family = r.Field<string>("FAMILY"),
bycode = r.Field<string>("BAYGANI"),
jancode = r.Field<string>("CODEJANBAZ"),
darsad = r.Field<int>("DARSAD"),
ostan = r.Field<string>("OSTAN_N"),
vacode = r.Field<string>("VA_CODE"),
moin = r.Field<string>("VA_MOIN"),
onvan = r.Field<string>("TAFZILI"),
aslvam = r.Field<double>("ASLVAM"),
gest = r.Field<double>("GEST"),
//tededGestKol = Convert.ToInt32(r.Field<double>("ASLVAM") / r.Field<double>("GEST")),
mandeVam = r.Field<double>("MANDE_VAM"),
dPardakht = r.Field<string>("DATE_P")
};`<code>
我添加了reference System.Data.DataSetExtensions以使用CopyToDataTable()方法在dataGrid视图中显示查询结果,但该方法没有增加我的不确定性,
我也使用了来使用此方法,但这次我得到了以下错误:
“指定的强制转换无效”
请帮助我,我能做些什么来克服这个问题?CopyToDataTable()仅在查询返回IEnumerable 60时才起作用
orderby Convert.ToInt32(r.Field(“ASLVAM”)/r.Field(“GEST”))降序
选择r;
DataTable newDataTbl=ShowResult.CopyToDataTable();
var anonType=newDataTbl.AsEnumerable()
.选择(r=>new
{
pascode=r.字段(“PAS_代码”),
melli=r.字段(“CODEMELI”),
名称=r.字段(“NAM”),
族=r.字段(“族”),
bycode=r.字段(“BAYGANI”),
jancode=r.字段(“CODEJANBAZ”),
darsad=r.字段(“darsad”),
ostan=r.字段(“ostan_N”),
vacode=r.字段(“VA_代码”),
moin=右字段(“VA_moin”),
onvan=r.Field(“TAFZILI”),
aslvam=r.字段(“aslvam”),
gest=r.字段(“gest”),
//TEDEGESTKOL=Convert.ToInt32(r.Field(“ASLVAM”)/r.Field(“GEST”),
mandeVam=r.字段(“MANDE_VAM”),
dPardakht=r.字段(“日期”)
}
);
}
代替前一种方法,您可以使用以下扩展方法从列表创建DatatableCopyToDataTable()仅在查询返回IEnumerable时有效
orderby Convert.ToInt32(r.Field(“ASLVAM”)/r.Field(“GEST”))降序
选择r;
DataTable newDataTbl=ShowResult.CopyToDataTable();
var anonType=newDataTbl.AsEnumerable()
.选择(r=>new
{
pascode=r.字段(“PAS_代码”),
melli=r.字段(“CODEMELI”),
名称=r.字段(“NAM”),
族=r.字段(“族”),
bycode=r.字段(“BAYGANI”),
jancode=r.字段(“CODEJANBAZ”),
darsad=r.字段(“darsad”),
ostan=r.字段(“ostan_N”),
vacode=r.字段(“VA_代码”),
moin=右字段(“VA_moin”),
onvan=r.Field(“TAFZILI”),
aslvam=r.字段(“aslvam”),
gest=r.字段(“gest”),
//TEDEGESTKOL=Convert.ToInt32(r.Field(“ASLVAM”)/r.Field(“GEST”),
mandeVam=r.字段(“MANDE_VAM”),
dPardakht=r.字段(“日期”)
}
);
}
代替前一种方法,您可以使用以下扩展方法从列表创建Datatable谢谢,在代码末尾,我想将此Datatable绑定到datagriview,它没有任何问题吗?我使用dataGridView1.DataSource=anonType;为了在DataGridView中显示查询的数据,但没有显示任何内容,我使用了dataGridView1.DataSource=anonType.ToList();但这一次我收到了一条错误消息:“指定的强制转换无效”@ozzy\u mra要绑定到datagrid,可以使用DataView。创建表,然后使用DefaultView基于列数组创建新表。ToTable(false,new[]{“PAS_CODE”,“CODEMELI”,“etc…”);更多信息请参见此链接@ozy_mra如果您不想投影的数据表中的列列表很小,请尝试使用newDataTbl.columns.Remove(“ASLVAM”);grid.DataSource=newdatatable谢谢,在代码的最后,我想将这个datatable绑定到datagriview,它没有任何问题吗?我使用了dataGridView1.DataSource=anonType;为了在DataGridView中显示查询的数据,但没有显示任何内容,我使用了dataGridView1.DataSource=anonType.ToList();但这一次我收到了一条错误消息:“指定的强制转换无效”@ozzy\u mra要绑定到datagrid,可以使用DataView。创建表,然后使用DefaultView基于列数组创建新表。ToTable(false,new[]{“PAS_CODE”,“CODEMELI”,“etc…”);更多信息请参见此链接@ozy_mra如果您不想投影的数据表中的列列表很小,请尝试使用newDataTbl.columns.Remove(“ASLVAM”);grid.DataSource=newDataTable
public static void Start()
{
DataTable Result = new DataTable();
var ShowResult = from r in Result.AsEnumerable()
where Convert.ToInt32(r.Field<double>("ASLVAM") / r.Field<double>("GEST")) > 60
orderby Convert.ToInt32(r.Field<double>("ASLVAM") / r.Field<double>("GEST")) descending
select r;
DataTable newDataTbl = ShowResult.CopyToDataTable();
var anonType = newDataTbl.AsEnumerable()
.Select(r => new
{
pascode = r.Field<string>("PAS_CODE"),
melli = r.Field<string>("CODEMELI"),
name = r.Field<string>("NAM"),
family = r.Field<string>("FAMILY"),
bycode = r.Field<string>("BAYGANI"),
jancode = r.Field<string>("CODEJANBAZ"),
darsad = r.Field<int>("DARSAD"),
ostan = r.Field<string>("OSTAN_N"),
vacode = r.Field<string>("VA_CODE"),
moin = r.Field<string>("VA_MOIN"),
onvan = r.Field<string>("TAFZILI"),
aslvam = r.Field<double>("ASLVAM"),
gest = r.Field<double>("GEST"),
//tededGestKol = Convert.ToInt32(r.Field<double>("ASLVAM") / r.Field<double>("GEST")),
mandeVam = r.Field<double>("MANDE_VAM"),
dPardakht = r.Field<string>("DATE_P")
}
);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Reflection;
namespace Common
{
public static class DataTableExtensions
{
public static DataTable ConvertToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
table.AcceptChanges();
return table;
}
public static DataRow ConvertToDataRow<T>(this T item, DataTable table)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
return row;
}
public static T ConvertToEntity<T>(this DataRow tableRow) where T : new()
{
// Create a new type of the entity I want
Type t = typeof(T);
T returnObject = new T();
foreach (DataColumn col in tableRow.Table.Columns)
{
string colName = col.ColumnName;
// Look for the object's property with the columns name, ignore case
PropertyInfo pInfo = t.GetProperty(colName.ToLower(),
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
// did we find the property ?
if (pInfo != null)
{
object val = tableRow[colName];
// is this a Nullable<> type
bool IsNullable = (Nullable.GetUnderlyingType(pInfo.PropertyType) != null);
if (IsNullable)
{
if (val is System.DBNull)
{
val = null;
}
else
{
// Convert the db type into the T we have in our Nullable<T> type
val = Convert.ChangeType(val, Nullable.GetUnderlyingType(pInfo.PropertyType));
}
}
else
{
// Convert the db type into the type of the property in our entity
SetDefaultValue(ref val, pInfo.PropertyType);
if (pInfo.PropertyType.IsEnum && !pInfo.PropertyType.IsGenericType)
{
val = Enum.ToObject(pInfo.PropertyType, val);
}
else
val = Convert.ChangeType(val, pInfo.PropertyType);
}
// Set the value of the property with the value from the db
if (pInfo.CanWrite)
pInfo.SetValue(returnObject, val, null);
}
}
// return the entity object with values
return returnObject;
}
private static void SetDefaultValue(ref object val, Type propertyType)
{
if (val is DBNull)
{
val = GetDefault(propertyType);
}
}
public static object GetDefault(Type type)
{
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}
public static List<T> ConvertToList<T>(this DataTable table) where T : new()
{
Type t = typeof(T);
// Create a list of the entities we want to return
List<T> returnObject = new List<T>();
// Iterate through the DataTable's rows
foreach (DataRow dr in table.Rows)
{
// Convert each row into an entity object and add to the list
T newRow = dr.ConvertToEntity<T>();
returnObject.Add(newRow);
}
// Return the finished list
return returnObject;
}
}
}