Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
While using join condition we are facing this type error.Near Join- The type of one of the expression in the join clause is incorrect.Type inference failed in the call to 'GroupJoin'.

Near foreach-Cannot convert type 'Anonymous Type#1' to 'employee.Employee'.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace employee
{
    public class Department
    {
        public int deptId { get; set; }
        public string deptName { get; set; }
    }
    public class Employee
    {
        private employee.Department dept;

        public Employee(employee.Department dept)
        {
            // TODO: Complete member initialization
            this.dept = dept;
        }
        public int empId { get; set; }
        public string empName { get; set; }
        public int empSalary { get; set; }
        public Department Department { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
      {
            ArrayList list = new ArrayList();
            Department dept = new Department();
            dept.deptId = 1;
            dept.deptName = "IT";
            list.Add(dept);
            ArrayList list1 = new ArrayList();
            Employee emp = new Employee(dept);
            emp.empId = 100;
            emp.empName = "anusha";
            emp.empSalary = 10000;
            emp.Department = dept;
            list1.Add(emp);
            dept = new Department();
            dept.deptId = 2;
            dept.deptName = "HR";
            list.Add(dept);
            emp = new Employee(dept);
            emp.empId = 101;
            emp.empName = "usha";
            emp.empSalary = 20000;
            emp.Department = dept;
            list1.Add(emp);
            var query = from Employee employee in list1
                        where employee.empId == 101
                        select employee;
            foreach(Employee e in query)
            {
                Console.WriteLine("EmpID\tEmpName\tEmpSal\tDepartmentID");
                Console.WriteLine(e.empId+"\t"+e.empName +"\t"+e.empSalary+"\t"+e.Department.deptId+"");
            }
            var query1=(from Employee employee1 in list1 
                       join Department department1 in list 
                       on employee1.Department equals department1.deptId into o
                       from department1 in o.DefaultIfEmpty()
                       select new
                       {
                           id=employee1.empId,
                           name=employee1.empName,
                           salary=employee1.empSalary,
                           departmentname=department1.deptName
                       });
            foreach (Employee e1 in query1)
            {
                Console.WriteLine("EmpID\tEmpName\tEmpSal\tDepartmentID");
                Console.WriteLine(e1.empId + "\t" + e1.empName + "\t" + e1.empSalary + "\t" + e1.Department.deptName + "");
            }
            Console.ReadLine();
        }
    }
}


Any Input..
Thanks
Sindhu
Posted

1 solution

C#
var query1 = (from Employee employee1 in list1 
             join Department department1 in list 
             on employee1.Department equals department1.deptId into o
             from department1 in o.DefaultIfEmpty()
             select new Employee()
             {
                id=employee1.empId,
                name=employee1.empName,
                salary=employee1.empSalary,
                departmentname=department1.deptName
             });


You may have to adjust properties names according to how you defined the Employee class. I leave it as an exercise for you so that you can clearly understand how linq queries and results work.
 
Share this answer
 
Comments
sindhukrothapalli 7-Jan-14 4:58am    
Thanks it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900