. sum()方法的查询
本文关键字:查询 方法 sum | 更新日期: 2023-09-27 17:51:24
int sum = this.baseClass.PhoneCalls.Sum(phonecall => phonecall.Seconds);
Show(this.baseClass.PhoneCalls
.Where(phonecall => this.baseClass.Employees
.Where(employee => employee.Department.Name.Equals("Finanzas"))
.Any(employee => employee.TelephoneNumber.Equals(phonecall.DestinationNumber)));
你好,
我正试图对一个特定部门的电话进行求和,但是我只是对所有电话进行了求和,没有对该部门进行筛选。
我不知道如何将sum变量包含到Show()中,所以它只是对来自特定部门的人员的电话进行求和。
类(员工)public class Employee {
public Employee(string name, string surname, DateTime dateOfBirth, string telephoneNumber, string email, string province, Office office) {
Name = name;
Surname = surname;
Email = email;
TelephoneNumber = telephoneNumber;
DateOfBirth = dateOfBirth;
Province = province;
Office = office;
}
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public string TelephoneNumber { get; set; }
public DateTime DateOfBirth { get; set; }
public string Province { get; set; }
public Office Office { get; set; }
public Department Department { get; set; }
public int Age { get { return (DateTime.Now - DateOfBirth).Days / 365; } }
public override string ToString() {
return String.Format("[Employee: {0}]", Name);
}
}
(来电)public class PhoneCall {
public DateTime Date { get; set; }
public string SourceNumber { get; set; }
public string DestinationNumber { get; set; }
public int Seconds { get; set; }
public override string ToString() {
return String.Format("[Phone call: from {0} to {1}]", SourceNumber, DestinationNumber);
}
}
(部门)public class Department {
public Department(string name, IEnumerable<Employee> employees) {
Name = name;
Employees = employees;
}
public string Name { get; private set; }
public IEnumerable<Employee> Employees { get; private set; }
public override string ToString() {
return String.Format("[Department: {0}]", Name);
}
}
问候。
我猜你有麻烦,因为Any
返回bool
..你想过滤…因此,在Where
和Sum
之后添加另一个条件:
Show(this.baseClass.PhoneCalls
.Where(phonecall => this.baseClass.Employees
.Where(employee => employee.Department.Name.Equals("Finanzas") &&
employee.TelephoneNumber.Equals(phonecall.DestinationNumber)))
.Sum(phonecall => phonecall.Seconds));