我所说的返回类型

时间:2017-02-01 17:29:33

标签: c# linq

这是我的表格列

 public partial class Dept_Emp
    {
        public int Id { get; set; }
        public string F_Name { get; set; }
        public string L_Name { get; set; }
        public string Gender { get; set; }
        public string DeprtmentName { get; set; }
        public Nullable<decimal> Sal { get; set; }
    }

当我尝试将上面的表格转换为Liq时,我为什么会收到错误

CS0029无法隐式转换类型'System.Collections.Generic.List&lt;&gt;'  到'System.Tuple'

Public IEnumarable<Dept_Emp> GetEmp(){
    var x = (from de in db.Dept_Emp
                         group de by de.DeprtmentName into g
                         select new
                         {
                             DeprtmentName = g.Key,
                             TotalSalary = g.Sum(x => x.Sal)
                         });
//Here wat I pass
}

1 个答案:

答案 0 :(得分:0)

不允许在返回类型中使用匿名类型。因此,您需要为结果值创建显式类型:

// type to hold the result
public class SalariesPerDepartment
{
     public string DepartmentName { get;set; }
     public decimal? TotalSalary { get; set; }
}

                     // use type here
public IEnumarable<SalariesPerDepartment> GetEmp()
{
    return (from de in db.Dept_Emp
             group de by de.DeprtmentName into g
             select new SalariesPerDepartment // instantiate that type
             {
                 DeprtmentName = g.Key,
                 TotalSalary = g.Sum(x => x.Sal)
             });
}