locked
how to iterate on dictionary which has a list of values? RRS feed

  • Question

  • User-1104215994 posted

    Hello;

    I have a sample dictionary as follows:

    Dictionary<strign, List<string>> recon= new Dictionary<strign, List<string>>();
    
    List<string> referenceId= new List<string>();
    referenceId.Add("0001"); 
    referenceId.Add("0002"); 
    referenceId.Add("0003"); 
    referenceId.Add("0004");
    
    List<string> productCode= new List<string>();
    productCode.Add("abc"); 
    productCode.Add("def"); 
    productCode.Add("ght"); 
    productCode.Add("yuk");
    
    recon["referenceId"] = referenceId;
    recon["productCode"] = productCode;

    I am trying to iterate over only <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="9" data-gr-id="9">referenceId</g> values and get the values. How can I achieve it? By the way is there any shortcut version rather than using 2 for each <g class="gr_ gr_10 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="10" data-gr-id="10">loops</g>?

    var reconResult = await (context.GameBanks.Where(g => g.used == 0)
                    .Where(l => l.referenceId != null)
                    ).ToListAsync();
    
    if (reconResult.Count() != 0)
    {
            foreach (var item in reconResult)
            {
                  foreach (KeyValuePair<string, List<string>> entry in recon.Where(r => r.Key == "referenceId"))
    {
    if (item.referenceId == entry.Value[0])
    {
    //item.used = 1;
    }
    } }
    }

    Best Regards.

    Wednesday, April 17, 2019 6:24 PM

All replies

  • User-1104215994 posted

    This code works but is there any other way to <g class="gr_ gr_58 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="58" data-gr-id="58">simplifiy</g> or make more efficient?

    var reconResult = await (context.GameBanks.Where(g => g.used == 0)
                    .Where(l => l.referenceId != null)
                    ).ToListAsync();
    
                if (reconResult.Count() != 0)
                {
                    var matches = from val in recon where val.Key == "referenceId" select val.Value;
                    foreach (var item in reconResult)
                    {
                        foreach (var match in matches)
                        {
                            foreach (var reference in match)
                            {
                                if (item.referenceId == reference)
                                {
                                    item.used = 1;
                                    context.Entry(item).State = System.Data.Entity.EntityState.Modified;
                                }
                            }
    
                        }
    
                        
                    }
    
    
                    await context.SaveChangesAsync();
    
                }

    Thursday, April 18, 2019 6:03 AM
  • User-1174608757 posted

    Hi cenk1536,

    According to your description,  if you just want to loop the referenceId values and get it ,you could write as below,Here is the demo, I hope it could help you.

    Dictionary<string, List<string>> recon = new Dictionary<string , List<string>>();
    
                List<string> referenceId = new List<string>();
                referenceId.Add("0001");
                referenceId.Add("0002");
                referenceId.Add("0003");
                referenceId.Add("0004");
    
                List<string> productCode = new List<string>();
                productCode.Add("abc");
                productCode.Add("def");
                productCode.Add("ght");
                productCode.Add("yuk");
    
                recon["referenceId"] = referenceId;
                recon["productCode"] = productCode;
    
                foreach (string key in recon.Keys  )
                {
                    if (key == "referenceId")
                    {
                        foreach (string item in recon[key])
                        {
                           // Response.Write(item);
                        }
                    }
                   
                }

    Best Regards

    Wei

    Thursday, April 18, 2019 6:04 AM