Home > OS >  Adding values based on the order of elements in the list
Adding values based on the order of elements in the list

Time:09-07

I am returning a list of products and i need to assign to each product a value form another list based on the product position in the list.I need to add the Additional text from productOrder to productInfo based on the row and return productInfo still as a list and keep it in the same order.

This is the model that containts the value

 public class ProductOrder
    {
        public int Row { get; set; }

        public string AdditionalText { get; set; }
    }

And this is the Product list where i need the value

internal class ProductInfo
    {
        public bool Support { get; set; }

        public string IconUrl { get; set; }

        public string AdditionalText { get; set; }
    }

This is how i assign data for testing purposes

  static List<ProductOrder> ReturnProductorder()
        {
            var productAdditionalInfo = new List<ProductOrder>()
            {
                new  ProductOrder()
                {
                    Row = 0,
                    AdditionalText = "TEST"
                },
                new  ProductOrder()
                {
                    Row = 3,
                    AdditionalText = "TEST"
                },
                 new  ProductOrder()
                {
                    Row = 10,
                    AdditionalText = "TEST"
                }
            };
            return productAdditionalInfo;
        }

        static List<ProductInfo> ReturnProductInfo()
        {
            var productAdditionalInfo = new List<ProductInfo>()
            {
                new ProductInfo()
                {
                  IconUrl = "test1",
                  Support = false,
                },
                 new ProductInfo()
                {
                  IconUrl = "test2",
                  Support = true,
                },
                  new ProductInfo()
                {
                  IconUrl = "test3",
                  Support = true,
                },
                 new ProductInfo()
                {
                  IconUrl = "test4",
                  Support = true,
                }

            };
            return productAdditionalInfo;
        }

And here is my logic

static void Main(string[] args)
        {
            var productOrder = ReturnProductorder();
            var productInfo = ReturnProductInfo();

           for(int i = 0; i < productOrder.Count; i  )
           {
                if (!string.IsNullOrWhiteSpace(productOrder[i].AdditionalText))
                {
                    var test2 = productInfo.ElementAt(productOrder[i].Row);
                    test2.AdditionalText = productOrder[i].AdditionalText;
                }
           }
        }

CodePudding user response:

I'm not sure if this fixes the root of your issue, but it will fix the symptom. Check if productInfo.Count is greater than Row(the index):

for (int i = 0; i < productOrder.Count; i  )
{
    ProductOrder po = productOrder[i];
    if (!string.IsNullOrWhiteSpace(po.AdditionalText) && productInfo.Count > po.Row)
    {
        ProductInfo pi = productInfo.ElementAt(po.Row);
        pi.AdditionalText = po.AdditionalText;
    }
}

CodePudding user response:

Here's how you can get the same results using linq:

   var resultList = productInfo.Select((productInfo, index) =>
        {
            productInfo.AdditionalText = productOrder.Where(w=>w.Row== index).Select(o=>o.AdditionalText).FirstOrDefault();

            return productInfo;
        }).ToList();

I should mention that the resultList is a new list and has different reference than productinfo list.

  •  Tags:  
  • c#
  • Related