There are two lists

List<string> lst1 = new List<string>(){"Бренд:Zara","Вес:40гр","Размер:20*20*20"}; List<string> lst2 = new List<string>(){"Бренд:Mario Muzi","Вес:1230гр","Фасовка:1",}; 

In the end, I want to combine both lists to bring later in Excell. The only problem is to bring both lists into a single format so that the name of the columns is one. In the end, it should work. enter image description here

  • 2
    why not use the class that describes this table and use the list of objects of this class? if class variable = null, then the cell must be empty - Senior Pomidor
  • I initially do not know how many fields I will have, that’s the problem. - Rajab
  • one
    And how do you get the line ( "Brand: Zara", "Weight: 40gr", "Size: 20 * 20 * 20" )? - Bald

3 answers 3

 List<string> lst1 = new List<string>() { "Бренд:Zara", "Вес:40гр", "Размер:20*20*20" }; List<string> lst2 = new List<string>() { "Бренд:Mario Muzi", "Вес:1230гр", "Фасовка:1", }; List<string> titles = new List<string>(); lst1.ForEach(s => titles.Add(s.Substring(0, s.IndexOf(":", StringComparison.InvariantCultureIgnoreCase)))); lst2.ForEach(s => titles.Add(s.Substring(0, s.IndexOf(":", StringComparison.InvariantCultureIgnoreCase)))); titles = titles.Distinct().ToList(); //print titles .... titles.ForEach(t => Console.Write(t + "\t")); Console.WriteLine(); //For lst 1 titles.ForEach(t => { var str = lst1.Where(s => s.Contains(t)) .Select(s => s.Substring(t.Length+1)) .FirstOrDefault(); Console.Write(string.IsNullOrWhiteSpace(str)?"--\t":str+"\t"); }); Console.WriteLine(); //for lst2 //.... 
  • And why everywhere .ForEach ? Than titles.ForEach(t => Console.Write(t + "\t")); better than foreach (var t in titles) Console.Write(t + "\t"); ? - VladD
  • @ VladD Just prefer 1 line. The compiler will then deploy in a loop - Leonid Malyshev
  • Well, one line is possible and so and so. - VladD

You need to create a class describing the product, then combine the lists for example:

 List<Product> lst1 = new List<Product>() { new Product() { Brand = "Zara", Weight = 40, Size = "20*20*20" } }; List<Product> lst2 = new List<Product>() { new Product() { Brand = "Mario Muzi", Weight = 1230, Pre_Packing = 1 } }; List<Product> allList = lst1.Concat(lst2).ToList(); 

product class:

 public class Product { public string Brand { get; set; } public int Weight { get; set; } public string Size { get; set; } public int Pre_Packing { get; set; } } 
  • I initially do not know how many fields I will have, that’s the problem. - Rajab
  • one
    @Radzhab: This is a serious clarification. It makes sense to add a question. - VladD

My version will be similar to the offer from @Leonid Malyshev. To store the combined information I need the following auxiliary class:

 class MergedEntityInfo { public string Name; public string Value1; public string Value2; } 

Now suppose that the description format of the property "<name>: <value>" is always respected for both lists, then we will analyze the information stored in the first list:

 var mergedEntityInfoMap = (from entityDescription in lst1 let info = entityDescription.Split(':').ToArray() let entity = new MergedEntityInfo { Name = info[0], Value1 = info[1] } select entity) .ToDictionary(x => x.Name); 

Next, we will go through the second list and add to the information in mergedEntityInfoMap :

 foreach (var info in lst2.Select(x => x.Split(':').ToArray())) { MergedEntityInfo mergedEntityInfo; if (mergedEntityInfoMap.TryGetValue(info[0], out mergedEntityInfo)) { mergedEntityInfo.Value2 = info[1]; } else { mergedEntityInfoMap[info[0]] = new MergedEntityInfo { Name = info[0], Value2 = info[1] }; } } 

That's it, mergedEntityInfo contains combined information.