Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过匿名传递列名,对通用列表中的一列求和_C#_Linq_Generics - Fatal编程技术网

C# 通过匿名传递列名,对通用列表中的一列求和

C# 通过匿名传递列名,对通用列表中的一列求和,c#,linq,generics,C#,Linq,Generics,我的代码是这样的 private static void ShowSum(Label lbl, IEnumerable<dynamic> list, string columnName) { lbl.Text = list.Sum(x => x.columnName).ToString(); } private static void ShowSum(标签lbl、IEnumerable列表、字符串columnName) { lbl.Text=

我的代码是这样的

 private static void ShowSum(Label lbl, IEnumerable<dynamic> list, string columnName)
    {
        lbl.Text = list.Sum(x => x.columnName).ToString();
    }
private static void ShowSum(标签lbl、IEnumerable列表、字符串columnName)
{
lbl.Text=list.Sum(x=>x.columnName.ToString();
}

每次调用此函数时,我都试图获取特定列的和,列名将作为参数传递给此函数。到目前为止,这不起作用,因为
columnName
被解释为特定列名的字符串。实现这一点的正确方法是什么?

您需要使用反射从动态对象的名称中获取属性:

lbl.Text = list.Sum(x => x.GetType().GetProperty(columnName).GetValue(x, null)).ToString();

但我建议您使用强类型而不是动态类型。至少您可以在编译时而不是运行时捕获一些错误。

您需要使用反射从动态对象中按其名称获取属性:

lbl.Text = list.Sum(x => x.GetType().GetProperty(columnName).GetValue(x, null)).ToString();

但我建议您使用强类型而不是动态类型。至少您可以在编译时而不是运行时捕获一些错误。

如果您知道在运行时键入列表:

private static void ShowSum<T>(Label lbl, IEnumerable<T> list, Func<T, int> columnMapper)
    {
        lbl.Text = list.Sum(x => columnMapper(x)).ToString();
    }
private static void ShowSum(标签lbl、IEnumerable list、Func columnMapper)
{
lbl.Text=list.Sum(x=>columnMapper(x)).ToString();
}

如果您知道在运行时输入列表类型:

private static void ShowSum<T>(Label lbl, IEnumerable<T> list, Func<T, int> columnMapper)
    {
        lbl.Text = list.Sum(x => columnMapper(x)).ToString();
    }
private static void ShowSum(标签lbl、IEnumerable list、Func columnMapper)
{
lbl.Text=list.Sum(x=>columnMapper(x)).ToString();
}

您是在编译时知道字符串(和列表类型)还是在运行时知道列名?您是在编译时知道字符串(和列表类型)还是在运行时知道列名?不必知道类型:对于参数,我们可以使用
Func columnMapper
,然后对于总和选择器,
x=>Convert.ToDouble((对象)columnMapper(x))
。这将接受任何匿名类型,并允许列返回任何数字类型(或null)。这是事实,但在不需要动态类型的情况下,我们可以获得更好的性能(在这种情况下,因为您需要反射)如果我们避免使用动态类型,则类型安全。不必知道类型:对于参数,我们可以使用
Func columnMapper
,然后对于总和选择器,
x=>Convert.ToDouble((对象)columnMapper(x))
。这将接受任何匿名类型,并允许列返回任何数字类型(或null)。这是事实,但在不需要动态类型的情况下,如果我们避免使用动态类型,我们可以获得更好的性能(在这种情况下,因为您需要反射)和类型安全性。