1

我在 linq 中编写了一个查询,根据员工编号返回 10 个不同员工的信息。不过,我是使用 linq 工作的新手,但在退货时遇到了问题。我在这段代码中做错了什么?

 public class queryClass
    {
        private string firstName { get; set; }
        private string lastName { get; set; }
        private int employeeNo { get; set; }
        private string department { get; set; }
    }

    public static queryClass[] GetEmpData(int empID1, int empID2, int empID3, int empID4, int empID5, int empID6, int empID7, int empID8, int empID9, int empID10)
    {

        using(var context = new EmpInfoDataContext())
        {
            var query = from e in context.EmployeTable
                        join j in context.JobTitles on e.JobTitle equals j.JobTitle
                        where e.EmployeeNo == empID1
                        where e.EmployeeNo == empID2
                        where e.EmployeeNo == empID3
                        where e.EmployeeNo == empID4
                        where e.EmployeeNo == empID5
                        where e.EmployeeNo == empID6
                        where e.EmployeeNo == empID7
                        where e.EmployeeNo == empID8
                        where e.EmployeeNo == empID9
                        where e.EmployeeNo == empID10
                        select new  {e.FirstName, e.LastName, e.EmployeeNo, j.Department};

            return query.ToArray();
        }
    }

在“return query.Toarray();”下,错误是“无法将表达式类型 '{FirstName:string, LastName:string, EmployeeNo:int, Department:string}[] 转换为返回类型 'Employeephotos.Models.HomeModel.queryClass []"

谢谢你提供的所有帮助。

4

2 回答 2

2

您的 where 子句应该是一个布尔表达式。

where e.EmployeeNo == empID1 || e.EmployeeNo == empID2 ... etc

并确保您声明所选项目的类型

select new queryClass {
    firstName = e.FirstName,
    lastName = e.LastName,
    employeeNo = e.EmployeeNo,
    department = j.Department
};
于 2013-02-12T18:26:24.607 回答
2

select new {e.FirstName, e.LastName, e.EmployeeNo, j.Department};正在返回匿名类型

将其更改为返回 queryClass

select new  queryClass {firstName = e.FirstName, lastName =  e.LastName, employeeNo =e.EmployeeNo, department = j.Department};
于 2013-02-12T18:35:00.377 回答