join子句中某个表达式的类型不正确。在对“GroupJoin”的调用中,类型推断失败。 [英] The type of one of the expression in the join clause is incorrect.Type inference failed in the call to 'GroupJoin'.

查看:152
本文介绍了join子句中某个表达式的类型不正确。在对“GroupJoin”的调用中,类型推断失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用连接条件时,我们面临这种类型错误.Near Join- join子句中某个表达式的类型不正确。在对GroupJoin的调用中,类型推断失败。



近foreach- 无法将'匿名类型#1'转换为'employee.Employee'。

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'.

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





任何输入..

谢谢

Sindhu



Any Input..
Thanks
Sindhu

推荐答案

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





您可能必须根据您定义Employee类的方式调整属性名称。我将它作为练习留给您,以便您可以清楚地了解linq查询和结果的工作原理。



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.


这篇关于join子句中某个表达式的类型不正确。在对“GroupJoin”的调用中,类型推断失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆