【发布时间】:2016-09-16 11:54:35
【问题描述】:
我想连接两个对象并将它们组合为一个并使用 LINQ 执行平均查询。以下是我的课程
public class emp
{
public int empID { get; set; }
public string empName { get; set; }
}
public class salaries
{
public int empID { get; set; }
public string month { get; set; }
public int salary { get; set; }
}
public void repository()
{
lstemp = new List<emp> {
new emp{empID=1,empName="A"},
new emp{empID=2,empName="B"},
new emp{empID=3,empName="C"}
};
lstsalary = new List<salaries> {
new salaries{empID=1,month="jan",salary=1000},
new salaries{empID=1,month="feb",salary=1500},
new salaries{empID=1,month="mar",salary=2000},
new salaries{empID=2,month="jan",salary=1500},
new salaries{empID=2,month="feb",salary=2000},
new salaries{empID=3,month="jan",salary=1500},
new salaries{empID=3,month="feb",salary=2000},
new salaries{empID=3,month="mar",salary=2000},
new salaries{empID=3,month="apr",salary=2000},
};
}
static void Main()
{
new calculator().repository();
var result = from e in calculator.lstemp
join
s in calculator.lstsalary on e.empID equals s.empID
group s by s.empID into grp
select new {
salary = (from g in grp where g.empID == grp.Key select g.salary).Average()
};
}
现在从上述数据中,我想根据 empID 进行分组,然后我想从他们各自的工资中取平均值。
结果应该是 empName 和平均工资 员工。
如何实现这一点。 谢谢。
【问题讨论】:
-
顺便说一句,现在是了解并开始遵循 .NET 命名约定的好时机。
标签: linq