none
Compare two list RRS feed

  • Question

  • Hello, I wanted to make this question, I want to go through two lists just to compare a field, and get the UserCreated, and the UserUpdated from the second list, this new result I put in a third list that is the final result,
    It works well
    I wanted to know if there was a shorter and easier way to do it maybe with linq

    BEResponseSql<BEProject> listaProjectFinal = new BEResponseSql<BEProject>();
    listaProjectFinal.Generic = new List<BEProject>();
    
    BEResponseSql<BEProject> rList = getData();
    BEResponseService<BELiderOwner> lista = Contenedor.Get();
    
    foreach (BELider lider in lista.ListaLider)
    {
    foreach (BEProject project in rList.Generic)
    {
    	if (lider.EMPLOYEE_ID == project.UserCreated.Substring(2, 4).PadLeft(10, '0'))
    	{
    		BEProject beProject = new BEProject();
    		beProject.IDProject = project.IDProject;
    		beProject.Name = project.Name;
    		beProject.Description = project.Description;
    		beProject.Objective = project.Objective;
    		beProject.Benefit = project.Benefit;
    		beProject.IDOwner = project.IDOwner;
    		beProject.IDExecutor = project.IDExecutor;
    		beProject.IDLiderOwner = project.IDLiderOwner;
    		beProject.IDLiderExecutor = project.IDLiderExecutor;
    		beProject.IDProjectState = project.IDProjectState;
    		beProject.IDProjectType = project.IDProjectType;
    		beProject.DateCreated = project.DateCreated;
    		beProject.UserCreated = lider.EMPLOYEE_NAME;
    		beProject.DateUpdate = project.DateUpdate;
    		beProject.UserUpdate = string.IsNullOrEmpty(project.UserUpdate )? String.Empty:lider.EMPLOYEE_NAME;
    		beProject.IDProjectType = project.IDProjectType;
    		listaProjectFinal.Generic.Add(beProject);
    	}
    }
    }
      return Json(listaProjectFinal, JsonRequestBehavior.AllowGet);





    • Edited by Augusto C Tuesday, September 24, 2019 4:42 PM
    Tuesday, September 24, 2019 4:06 PM

All replies

  • Hi Augusto C,

    Thank you for posting here.

    According to your code and description, I think you want to get the UserCreated and the UserUpdated from the second list based on the comparison, then modify the first list based on these elements, and put the results into the third list , right?

    Because I don't have your complete code, I have to create two new entity classes and two collections based on your code. 

    It may be different from yours, but I think these code can give you help.

    Code:

    class Lider
        {
            public String Employee_Id { get; set; }
            public String Employee_Name { get; set; }
            public Lider(String id,String name)
            {
                Employee_Id = id;
                Employee_Name = name;
            }
        }
    
        class BEProject
        {
            public int IDProject { get; set; }
            public String Name { get; set; }
            public String UserCreated { get; set; }
            public String UserUpdate { get; set; }
            public BEProject(int id, String name, String userCreate, String userUpdate)
            {
                IDProject = id;
                Name = name;
                UserCreated = userCreate;
                UserUpdate = userUpdate;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                List<Lider> liders = GetLiders();
                List<BEProject> bEProjects = GetProjects();
    
                List<BEProject> results = new List<BEProject>();
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
                var result = from pro in bEProjects
                             join lider in liders
                             on pro.UserCreated.Substring(3,1) equals lider.Employee_Id
                             select new
                             {
                                 BEProject = pro,
                                 UserCreate = lider.Employee_Name,
                                 UserUpdate  = string.IsNullOrEmpty(pro.UserUpdate) ? String.Empty : lider.Employee_Name
                             };
                foreach (var item in result)
                {
                    BEProject bEProject = item.BEProject;
                    bEProject.UserCreated = item.UserCreate;
                    bEProject.UserUpdate = item.UserUpdate;
                    results.Add(bEProject);
                }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                foreach (var project in results)
                {
                    Console.WriteLine(project.IDProject+"   "+project.Name+"    "+project.UserCreated+"  "+project.UserUpdate);
                }
    
                Console.ReadKey();
            }
    
            public static List<Lider> GetLiders()
            {
                List<Lider> liders = new List<Lider>();
                liders.Add(new Lider("1","lider1"));
                liders.Add(new Lider("2", "lider2"));
                liders.Add(new Lider("3", "lider3"));
                return liders;
            }
    
            public static List<BEProject> GetProjects()
            {
                List<BEProject> bEProjects = new List<BEProject>();
                bEProjects.Add(new BEProject(100,"first","id=1",""));
                bEProjects.Add(new BEProject(101,"second","id=2","2"));
                bEProjects.Add(new BEProject(102,"third","id=3",String.Empty));
                bEProjects.Add(new BEProject(103,"fourth","id=3","3"));
                bEProjects.Add(new BEProject(104,"fifth","id=4","3"));
                return bEProjects;
            }
        }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, September 25, 2019 2:55 AM
    Moderator
  • Try adjusting an approach like this:

     

    listaProjectFinal.Generic =

        lista.ListaLider.Join(

            rList.Generic,

            l => l.EMPLOYEE_ID,

            p => p.UserCreated.Substring( 2, 4 ).PadLeft( 10, '0' ),

            ( l, p ) => new BEProject

            {

                IDProject = p.IDProject,

                Name = p.Name,

                // . . .

            } ).ToList( );

    Wednesday, September 25, 2019 5:08 AM