2
我想加入兩個對象,並將它們組合爲一個對象,並使用LINQ執行平均查詢。下面是我的類如何在使用LINQ的C#對象上使用Join,GroupBy和Average應用
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()
};
}
現在從i根據EMPID,然後我想從thier各自薪金取平均值要組以上的數據。
結果應該是僱員的僱員姓名和平均工資。
中怎樣才能做到這一點。 謝謝。
順便說一句,現在是一個很好的學習時間關於並開始遵循.NET命名約定。 –