[英]Pass type as parameter to LINQ Field method
下面我们有一个模拟数据库调用列表和一个小程序,它接受该列表并转换它做一个DataTable。 在这个例子中,我使用列名的变量来访问该列的值并获得平均值。 但是,我调用了Field
方法并给它了int类型。 似乎不可能将变量传递给通用Field
方法。 有没有其他方法可以访问DataTable的列值并返回类似平均值的内容而不知道列的类型直到运行时?
public class Icd
{
public int ConditionCode { get; set; }
public string ConditionName { get; set; }
public static List<Icd> GetIcdList()
{
return new List<Icd>()
{
new Icd() { ConditionCode = 111, ConditionName = "Condition 1" },
new Icd() { ConditionCode = 222, ConditionName = "Condition 2" },
};
}
}
var icdList = Icd.GetIcdList();
var columnName = "ConditionCode";
DataTable dt = new DataTable();
dt = icdList.ToList().ListToDataTable();
var avg = dt.AsEnumerable().Where(x => x[columnName] != DBNull.Value)
//possible to pass a variable to the field method?
.Average(x => x.Field<int>(columnName));
Console.WriteLine(avg); //<-- correct answer
更新:我试图添加:
Type t = typeof(int)
并做
x => x.Field<t>(columnName)
但这给了我错误:
找不到类型或命名空间't'
ListToDataTable帮助方法:
public static DataTable ListToDataTable<T>(this IList<T> data)
{
DataTable dt = new DataTable();
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
dt.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T t in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(t);
}
dt.Rows.Add(values);
}
return dt;
}
我想你可以在这里使用dynamic
类型。
例如:
var avg = dt.AsEnumerable().Where(x => x[columnName] != DBNull.Value)
//possible to pass a variable to the field method?
.Average(x => x.Field<dynamic>(columnName));
我做了最小的测试,似乎工作。 欢迎其他人对此发表评论。
干杯
必须在编译时知道泛型类型。 如果要创建该变量,则需要使用反射:
// Get the method information
MethodInfo method = typeof(T).GetMethod("Field");
// here hardcoded for int, but you could use any type
var types = new Type[] {typeof(int)};
// Create a generic instance of that method
MethodInfo genericMethod = method.MakeGenericMethod(types);
var avg = dt.AsEnumerable().Where(x => x[columnName] != DBNull.Value)
// Use the generic method with target x and parameter columnName
.Average(x => genericMethod.Invoke(x, columnName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.