C# 如何编写可用作 GetAverageAge(students, s => s.age) 的函数体

发布于 2024-11-02 03:56:29 字数 615 浏览 0 评论 0原文

假设我有几个不同类的 ObservableCollections:

    public class Employee
    {
        public int age { get; set; }
    }

    public class Student
    {
        public int age { get; set; }
    }

ObservableCollection<Employee> employees = ...;
ObservableCollection<Student> students = ...;

现在我需要一个函数来计算这些集合的平均年龄:

int employeeAveAge = GetAverageAge(employees, e => e.age);
int studentAveAge = GetAverageAge(students, s => s.age);

如何编写函数体?我不熟悉 Action/Fun 委托,有人建议我传递 lambda 作为函数的参数

,我不使用内置 LINQ Average() 因为我想学习将 lambda 传递给函数的用法

Say I have several ObservableCollections of different classes:

    public class Employee
    {
        public int age { get; set; }
    }

    public class Student
    {
        public int age { get; set; }
    }

ObservableCollection<Employee> employees = ...;
ObservableCollection<Student> students = ...;

now I need a function to calculation the average age of these collections:

int employeeAveAge = GetAverageAge(employees, e => e.age);
int studentAveAge = GetAverageAge(students, s => s.age);

How to write the function body? Im not familiar with Action/Fun delegate, and somebody suggested me to pass a lambda as the function's parameter

well I don't use the build-in LINQ Average() because I want to learn the usage of passing lambda to function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断桥再见 2024-11-09 03:56:29

该函数将是这样的(未经测试):

public int GetAverageAge<T>(IEnumerable<T> list, Func<T,int> ageFunc)
{

   int accum = 0;
   int counter = 0;
   foreach(T item in list)
   {
      accum += ageFunc(item);
      counter++;
   }

   return accum/counter;
}

您可以使用 LINQ 取而代之的是 Average 方法:

public int GetAverageAge<T>(IEnumerable<T> list, Func<T,int> ageFunc)
{
    return (int)list.Average(ageFunc);
}

The function would be something like this (untested):

public int GetAverageAge<T>(IEnumerable<T> list, Func<T,int> ageFunc)
{

   int accum = 0;
   int counter = 0;
   foreach(T item in list)
   {
      accum += ageFunc(item);
      counter++;
   }

   return accum/counter;
}

You could use the LINQ Average method instead:

public int GetAverageAge<T>(IEnumerable<T> list, Func<T,int> ageFunc)
{
    return (int)list.Average(ageFunc);
}
源来凯始玺欢你 2024-11-09 03:56:29

我会完全取消该功能,只使用:

int employeeAge = (int)employees.Average(e => e.age);
int studentAge = (int)students.Average(e => e.age);

编辑:
将返回和强制转换添加到 int(Average 返回 double)。

I'd do away with the function altogether and just use:

int employeeAge = (int)employees.Average(e => e.age);
int studentAge = (int)students.Average(e => e.age);

Edit:
Added the return and the cast to an int (Average returns a double).

握住你手 2024-11-09 03:56:29

您可以这样做:

    double GetAverageAge<T>(IEnumerable<T> persons, Func<T, int> propertyAccessor)
    {
        double acc = 0.0;
        int count = 0;
        foreach (var person in persons)
        {
            acc += propertyAccessor(person);
            ++count;
        }
        return acc / count;
    }

作为替代方案,请考虑使用 LINQ,它已经提供了类似的东西。

You could do something like this:

    double GetAverageAge<T>(IEnumerable<T> persons, Func<T, int> propertyAccessor)
    {
        double acc = 0.0;
        int count = 0;
        foreach (var person in persons)
        {
            acc += propertyAccessor(person);
            ++count;
        }
        return acc / count;
    }

As an alternative, consider using LINQ, it already provides something like this.

°如果伤别离去 2024-11-09 03:56:29

为什么不使用 LINQ 来实现这个目的呢?

employees.Select(e => e.age).Average()

students.Select(s => s.age).Average()

Why not use LINQ for this?

employees.Select(e => e.age).Average()

students.Select(s => s.age).Average()
脸赞 2024-11-09 03:56:29
I Would do it this way for a normal list not sure about observable collection :

       List<Employee> employees = new List<Employee>
                                       {
                                           new Employee{age = 12},
                                           new Employee{age = 14}};
        Func<List<Employee>, int> func2 = (b) => GetAverageAge(b);

private static int GetAverageAge(List<Employee> employees)
    {
        return employees.Select(employee => employee.age).Average; //Thats quicker i guess :)
    }
I Would do it this way for a normal list not sure about observable collection :

       List<Employee> employees = new List<Employee>
                                       {
                                           new Employee{age = 12},
                                           new Employee{age = 14}};
        Func<List<Employee>, int> func2 = (b) => GetAverageAge(b);

private static int GetAverageAge(List<Employee> employees)
    {
        return employees.Select(employee => employee.age).Average; //Thats quicker i guess :)
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文