Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

C# Linq Update One List<T> with values from another List<T>

I want to updated the LeadTime field in ComponentModel from LeadTimeModel where the PartNo's match.  They are both List<T>.
    public class ComponentsModel
    {
        public string PartNo { get; set; }
        public string Cost { get; set; }
        public string LeadTime { get; set; }
    }

Open in new window

    public class LeadTimeModel
    {
        public string PartNo { get; set; }
        public long LeadTime { get; set; }
    }

Open in new window

Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

The source code is in here: https://bitbucket.org/j0rt3g4/expertsexchangesolutions


 static void Main()
        {
            List<ComponentModel> cm = new List<ComponentModel>();
            List<LeadTimeModel> lt = new List<LeadTimeModel>();

            //Filling up the Classes
            for (int i = 0; i < 10; i++)
            {
                cm.Add(new ComponentModel
                {
                    Cost = Convert.ToString(10 * i),
                    PartNo = Convert.ToString(100 * i),
                    LeadTime = "0",
                });

                lt.Add(new LeadTimeModel
                {
                    PartNo = Convert.ToString(100 * i),
                    LeadTime = i
                });

            }

            Console.WriteLine("ComponentModelList");
            PrintC(cm);
            Console.WriteLine("LeadTime");
            Print(lt);

            var fm = (from c in cm
                      from x in lt
                      where (c.PartNo == x.PartNo)
                      select new ComponentModel
                      {
                          Cost = c.Cost,
                          PartNo = c.PartNo,
                          LeadTime = Convert.ToString(x.LeadTime)
                      }).ToList();


            Console.WriteLine("Final Model");
            PrintC(fm);
            Console.ReadKey();

        }
        static void PrintC(List<ComponentModel> list)
        {
            Console.WriteLine("Cost\tPartNo\tLeadTime");
            foreach (var item in list)
            {
                Console.WriteLine($"{item.Cost}\t{item.PartNo}\t{item.LeadTime}");
            }
        }
        static void Print(List<LeadTimeModel> list)
        {
            Console.WriteLine("PartNo\tLeadTime");
            foreach (var item in list)
            {
                Console.WriteLine($"{item.PartNo}\t{item.LeadTime}");
            }
        }

Open in new window


Result:
User generated image
Avatar of CipherIS

ASKER

I had below.
_leadtimemodel.ForEach(x => _componentsmodel.First(y => y.PartNo == x.PartNo).LeadTime = x.LeadTime.ToString());

Open in new window

Problem occurred when a PartNo didn't match.  Didn't think I would run into a prob as I'm looking for PartNo's that match.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Saige!