Danger_Fox
Danger_Fox

Reputation: 449

Returning data from a Linq to SQL query as a custom data type

I've written a query in linq to return the information of 10 different employees based on the their employeenumber. I'm new to working wtih linq though and I'm having issues returning it. What am I doing wrong in this code?

 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();
        }
    }

Under the 'return query.Toarray();" the error is "Cannot convert expression type '{FirstName:string, LastName:string, EmployeeNo:int, Department:string}[] to return type 'Employeephotos.Models.HomeModel.queryClass[]"

Thanks for any help you can give.

Upvotes: 1

Views: 1244

Answers (2)

Flowerking
Flowerking

Reputation: 2581

select new {e.FirstName, e.LastName, e.EmployeeNo, j.Department}; is returning anonymous type

change it to return queryClass

select new  queryClass {firstName = e.FirstName, lastName =  e.LastName, employeeNo =e.EmployeeNo, department = j.Department};

Upvotes: 2

Michael Dunlap
Michael Dunlap

Reputation: 4310

Your where clause should be a single boolean expression.

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

and Make sure you declare the type of your selected items

select new queryClass {
    firstName = e.FirstName,
    lastName = e.LastName,
    employeeNo = e.EmployeeNo,
    department = j.Department
};

Upvotes: 2

Related Questions