Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following code:

var failed = new List<(IProductChecker<TModel> name, IDictionary<int, string[]> failedProducts)>();
foreach (var ids in batches)
{
   var batch = Check(names, ids.ToArray()); // List<(IProductChecker<TModel> checker, IDictionary<int, string[]> failedProducts)>()
   
   failed.AddRange(batch);
}


currently I am adding range, however as I iterate through batches, names can repeat with different failedProducts, so my intention would be to add those products to existing `name` of batch.

What I have tried:

Something like:

if (failed.Any(x => batch.Select(b => b.checker).Contains(x.checker)))
{ 
   // concat products to existing dictionary as such name exists
  // add to failed[name].failedProducts.Add(batch[name].failedProducts)
}
else
{  // add new failed if such name does not exists
   // failed.Add(...)
}


but I can't figure out how to do that. Maybe someone could give me a hint how to solve it?
Posted
Updated 5-Oct-22 11:00am
v2
Comments
Richard Deeming 5-Oct-22 4:14am    
If you only want one entry for each name, wouldn't it make sense for failed to be a dictionary? Dictionary<IProductChecker<TModel>, IDictionary<int, string[]>>

1 solution

Try something along these lines

C#
    Dictionary<int, string[]> failedDict = new() 
     { 
         { 0, new string[] { "A", "B", "C" } },
         { 1, new string[] { "D", "E", "F" } } 
     };
     Dictionary<int, string[]> batchDict = new() 
     { 
         { 3, new string[] { "J", "K", "L" } }, 
         { 2, new string[] { "G", "H", "I" } } 
     };
     Dictionary<int, string[]> batchDict2 = new() 
     {
         { 4, new string[] { "M", "N", "O" } }, 
         { 5, new string[] { "P", "Q", "R" } } 
     };

     List<(string name, IDictionary<int, string[]> failedProducts)> failedList = new() 
     { 
         ("Widget",failedDict) 
     };
     List<(string name, IDictionary<int, string[]> batch)> batchList = new() 
     { 
         ("Item", batchDict),
         ("Widget",batchDict2) 
     };
      foreach(var b in batchList)
      {
        bool isMatched=false;
         foreach(var (name, failedProducts) in failedList)
          {
           if(b.name == name)
            {
             //convert the dictionary in a batchList item
             //to a collection of type KeyValuePair
             var kvPairs = b.batch.ToList();
             //add the pairs to the failedList item's dictionary
             kvPairs.ForEach(kv => failedProducts.Add(kv.Key, kv.Value));
             isMatched = true;
             break;
            }
          }
          if(!isMatched)
           {
            //the batchList item is not in the failedList so add it
            failedList.Add(b);
           }
      }
      foreach(var (name, failedProducts) in failedList)
       {
         Console.WriteLine(name);
         foreach(var kvp in failedProducts)
          {
            Console.WriteLine($"{kvp.Key} {string.Join(',',kvp.Value)}");
          }
       }
/*
Widget
0 A,B,C
1 D,E,F
4 M,N,O
5 P,Q,R
Item
3 J,K,L
2 G,H,I
*/

 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900