如何获得分配给项目的员工名单a& b仅使用linq。 [英] how to get list of employees who is assigned to projects a & b using linq only.

查看:57
本文介绍了如何获得分配给项目的员工名单a& b仅使用linq。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要获得分配到多个项目的员工名单。



Emp Table



员工姓名薪水......



项目表



ProjectId PrjName EmpId .....



我试图获得解决方案,但我没有达到。



Hi All,

I need to get the list of employees who are assigned to multiple projects.

Emp Table

EmployeeId name Salary...

Project Table

ProjectId PrjName EmpId.....

I tried to get the solution but i failed to reach.

List<Employee> lstEmployee = new List<Employee>();
          List<Projects> lstProject = new List<Projects>();

          lstProject.Add(new Projects() { EmployeeId = 1, name = "a", projId = 1 });
          lstProject.Add(new Projects() { EmployeeId = 1, name = "b", projId = 2 });
          lstProject.Add(new Projects() { EmployeeId = 2, name = "b", projId = 2 });

          lstEmployee.Add(new Employee() { EmployeeId = 1, FirstName = "sd", LastName = "kd", Salary = 20000,  });
          lstEmployee.Add(new Employee() { EmployeeId = 2, FirstName = "sd", LastName = "kd", Salary = 10000 });

          lstEmployee.Add(new Employee() { EmployeeId = 3, FirstName = "sd", LastName = "kd", Salary = 25000 });
          lstEmployee.Add(new Employee() { EmployeeId = 4, FirstName = "sd", LastName = "kd", Salary = 30000 });
          lstEmployee.Add(new Employee() { EmployeeId = 5, FirstName = "sd", LastName = "kd", Salary = 5000 });
          lstEmployee.Add(new Employee() { EmployeeId = 6, FirstName = "sd", LastName = "kd", Salary = 10000 });


          var salaryEmpSum = from emp in lstProject
                             group emp by emp.EmployeeId into empg
                             select new
                             {
                                 Name = empg.Key,
                                 Salary = empg.Count(n=>n.name =="a" && n.name =="b"),


                             };







请建议。



在此先感谢。




Please suggest.

Thanks in advance.

推荐答案

为此,您使用 where 子句。使用where子句,可以在选择数据以进行进一步处理之前添加需要满足​​的条件。不幸的是,LINQ 其中子句不支持像SQL一样的 IN()运算符。



您可以这样写:

For that, you use where clause. Using the where clause, you can add the conditions that need to be fulfilled before the data is selected for further processing. The downside is that sadly, LINQ where clause doesn't support an IN() operator like SQL.

You may write it like this:
var salaryEmpSum = from emp in lstProject
                   where IsIncluded(emp.EmployeeId, emp)
                   select emp;

bool IsIncluded(int empId, Projects project) {
   // Logic to check if the employee is in project a and b
   // Something like
   if(project.Employees.Find(x => x.ID == empId) != null) {
      // Employee associated
      return true;
   }
   // Do for the second one
   return false;
}



使用此设计,您可能希望存储对每个员工执行的迭代,以确定它们是在多个项目还是单个项目上。



where子句(C#参考) [ ^ ]



旁注:



我看到你的代码的方式,它一直很差设计的。对我来说,找到编写代码的方法是非常困难和艰巨的任务。您的对象设计不合理。例如,项目类(如果我必须设计它)将是这样的:


Using this design, you may want to have to store the iterations performed on each employee to determine if they are on multiple projects or a single one.

where clause (C# Reference)[^]

Side note:

The way I see your code, it has been very poorly designed. For me, it was very hard and a difficult task to find a way to write the code. Your objects are not well designed. For instance, the project class (if I had to design it) would be something like this:

// Project class
class Project {
   public int ID { get; set; }
   public string Name { get; set; }
   public List<employee> Employees { get; set; }
}

// Employee class
class Employee { 
   public int ID { get; set; }
   public string Name { get; set; }
   public List<project> Projects { get; set; }
}
</project></employee>



现在好处是你的对象设计得更好,如果你有软件工程师,乍一看他会知道这两个有多对多 [ ^ ]彼此之间的关系,所以他会设计像这样的算法这些非常有效。




Now the benefit of this would be that your objects would be better designed and if you get a software engineer, in the very first glance he would know that these two have a "Many-to-many[^]" relation with each other so he would design the algorithms like these very efficiently.

var selected = from emp in lstEmployee
               where Selected(emp)
               select emp;

bool Select(Employee emp) {
   // Iterate over Projects collection, easily!
}


这篇关于如何获得分配给项目的员工名单a&amp; b仅使用linq。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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