将类型作为参数传递给LINQ Field方法

4
以下是模拟数据库调用列表和一个小程序,它将该列表转换为 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;
}

你是否遇到了错误?请与我们分享。 - Luc Morin
@LucMorin 请查看编辑。 - wootscootinboogie
这个链接可能对你有帮助:http://msdn.microsoft.com/zh-cn/library/bb396189%28v=vs.110%29.aspx。 - abatishchev
@abatishchev 古董机器正在运行 .NET 4.0 :( - wootscootinboogie
@wootscootinboogie:别担心!自 .NET 3.5 起就存在扩展方法(只需引用 System.Data.DataSetExtensions.dll 即可)。 - abatishchev
2个回答

2

我认为你可以在这里使用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));

我进行了最少的测试,似乎它可以工作。欢迎其他人对此进行评论。

干杯


我也是这么想的 :) 我将编写一个 TryGetAverage 方法,如果尝试方法是浮点型、双精度型、整型等类型,则调用 GetAverage - wootscootinboogie

1

泛型必须在编译时知道。如果您想让变量成为可变的,您需要使用反射:

// 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);

不确定反射会减慢多少速度,但我想我要做的是将列类型转换为动态类型,并使用TryGetAverage,如果所有值都是整数、双精度浮点数、浮点数等,则执行该操作。 - wootscootinboogie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接