简体   繁体   中英

how to compare two list from two different classes and compare particular conditions if its true store result in 3rd list

There are are three lists ProjectList, EmployeeList and ProjectEmployeeList..

private List<Project> ProjectList { get; set; } = new List<Project>(); // List for project
private List<Employee> EmployeeList { get; set; } = new List<Employee>();  //List for employee
private List<ProjectEmployee> ProjectEmployeeList { get; set; } = new List<ProjectEmployee>(); 
                                                       //List for Add employee to project

I want to Compare a condition such as 'ProjectID' must be there in ProjectList(already avialable)

and the 'ProjectEmployeeId' from ProjectEmployeeList Matches the value with ProjectId means

if 'ProjectId==ProjectEmployeeId' if true then it will add ProjectEmployeeId into

ProjectEmployeeList.

And same for EmployeeProjectId also means 'ProjectId==ProjectEmployeeId' and

'EmployeeID==EmployeeProjectId' these both conditions true then ProjectEmployeeId and

EmployeeProjectId added into ProjectEmployeeList

 public void AddEmployeeToProject(ProjectEmployee projectemployee) // to add employee to 
                                                                                project
    {
         ProjectBusinessLogic projectbusinessList = new ProjectBusinessLogic();//object of 
                                                                               projectlist
         EmployeeBusinessLogic employeebusinessList = new EmployeeBusinessLogic();//object of 
                                                                             employee list
if(projectbusinessList.GetProjects().Any(x => x.ProjectId == projectemployee.ProjectEmployeeId))
       // condition to check project id is already present or not
        {
            if (employeebusinessList.GetEmployees().Any(k => k.EmployeeId == projectemployee.EmployeeProjectid))
            {
                ProjectEmployeeList.Add(projectemployee); //both condition true then it will add the values
            }
        }
        }

but its not comparing the list and so values are not added in ProjectEmployeeList.. and I also want that its should not take repeated value..means there should not be like this

|project id=1|, |employee id=1|

||project id=1|, |employee id=1|

and I have to do this in List only cannot use HashSet or anything else...

is it correct to use any??

GetProjects() and GetEmployees() this to methods are for calling list from respective classes because lists are private there...

you must be used IComparable and make compare class and use it.

use this link Comparing two list of different types (reference types) using C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM