skip to Main Content

I have a C# dictionary with a key of statecode and a list of a classtype. Is there away I can take only those dictionary items that have a FilnameId = 7 , and copy only those items to a second dictionary? I would like to do this without iterating through the dictionary if possible. Any and all direction would be most appreciated. My dictionary and class are below.

-Jason

public class FirstReportsData
{
    public FirstReportsData() { }
    public int Id { get; set; }
    public string Policy { get; set; }
    public int FileNameId { get; set; }
    public string StateCode { get; set; }

}

Dictionary<string, List<FirstReportsData>> dicFsrFileNameId1 = 
    new Dictionary<string, List<FirstReportsData>>();

4

Answers


  1. There is no way to filter a dictionary by values properties without iterating through all items…even if you use Linq, there will still be a iteration happening behind the scene

    Login or Signup to reply.
  2. It depends on what do you mean by "iterating". If you don’t want to write for/foreach loops then you can go with LINQ:

    var result = dicFsrFileNameId1
        .Values
        .SelectMany(d => d)
        .Where(d => d.FileNameId == 7)
        .ToList();
    

    If you need to rebuild the dictionary then use GroupBy:

    var resDict = dicFsrFileNameId1
        .Values
        .SelectMany(d => d)
        .Where(d => d.FileNameId == 7)
        .GroupBy(d => d.StateCode)
        .ToDictionary(g => g.Key, g => g.ToList());
    

    Otherwise no, since your data structure is not a dictionary from FileNameId to collection of files, you will need to go through all the FirstReportsData stored in the dictionary.

    Login or Signup to reply.
  3. As previously has been mentioned, may you don’t want to make a traditional for loops but iteration will run under the hood.

    you can use:

    Dictionary<string, List<FirstReportsData>> dicFsrFileNameId1 = new Dictionary<string, List<FirstReportsData>>();
    
    
    List<FirstReportsData> list = new List<FirstReportsData>();
    list.Add(new FirstReportsData() { Id = 1, FileNameId = 1, Policy = "Fake", StateCode = "Fake" });
    list.Add(new FirstReportsData() { Id = 2, FileNameId = 2, Policy = "Fake", StateCode = "Fake" });
    list.Add(new FirstReportsData() { Id = 3, FileNameId = 3, Policy = "Fake", StateCode = "Fake" });
    list.Add(new FirstReportsData() { Id = 4, FileNameId = 4, Policy = "Fake", StateCode = "Fake" });
    list.Add(new FirstReportsData() { Id = 5, FileNameId = 5, Policy = "Fake", StateCode = "Fake" });
    list.Add(new FirstReportsData() { Id = 6, FileNameId = 6, Policy = "Fake", StateCode = "Fake" });
    list.Add(new FirstReportsData() { Id = 7, FileNameId = 7, Policy = "Fake", StateCode = "Fake" });
    
    
    dicFsrFileNameId1.Add("test", list);
    
    Dictionary<string, List<FirstReportsData>> dicFsrFileNameId2 = new Dictionary<string, List<FirstReportsData>>();
    
    dicFsrFileNameId2.Add("testOut", dicFsrFileNameId1.Select(o => o.Value).First().Where(o => o.FileNameId == 7).ToList());
    
    Login or Signup to reply.
  4. This code converts directly into a dictionary with the same structure. Technically, it is an iteration, but there is no for loop. It preserves the same structure and takes into account that the StateCode value is always the same for each list in the input dictionary.

        var otherDictionary = dicFsrFileNameId1
                .Values
                .Select(dataList => dataList.Where(data => data.FileNameId == 7).ToList())
                .ToDictionary(data => data.First().StateCode, data => data);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search