Home > front end >  Splitting a Dictionary with a list in half
Splitting a Dictionary with a list in half

Time:09-22

I have a C# dictionary defined as following..

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

The List within my dictionary is defined as follows:

public class FirstReportsData
{
  public FirstReportsData() { }

  public int    Id { get; set; }
  public int    FirstReportsItemId { get; set; }
  public string Policy { get; set; }
  public string CarrierCode { get; set; }
  public string StateCode { get; set; }
  public string RecordTypeCode { get; set; }
  public string RecordData { get; set; }

}

The string (key) in the dictionary maybe a value for example like say "07", "00", or "09" . How do I take only the first half of the list with the key "00" and put it into a new dictionary. I then would also like to take the second half of the list from key "00" and put it into a second dictionary. Any possible direction on the approach or syntax needed would be greatly appreciated.

CodePudding user response:

I would use .Take and .Skip to get the two parts of the list.

If you only need to split a single item, get the entry of the dictionary first and than split the list.

If you need ALL items of the dictionary, use .ToDictionary to create new Dictionary with the splitted lists.

Dictionary<string, List<FirstReportsData>> dict1 = new Dictionary<string, List<FirstReportsData>>();
dict1.Add("00", new() { new() { Id = 0 },  new() { Id = 1 },  new() { Id = 2 },  new() { Id = 3 },  new() { Id = 4 } });
dict1.Add("01", new() { new() { Id = 0 },  new() { Id = 1 },  new() { Id = 2 } });
dict1.Add("02", new() { new() { Id = 0 },  new() { Id = 1 },  new() { Id = 2 },  new() { Id = 3 },  new() { Id = 4 },  new() { Id = 5 } });

//if you only want 1 spefic key
if (dict1.TryGetValue("00", out var values))
{
    var firstHalf = values.Take(values.Count / 2);
    var secondHalf = values.Skip(values.Count / 2);

    Console.WriteLine("First Half of 00");
    foreach (var item in firstHalf)
        Console.WriteLine(item.Id);

    Console.WriteLine("Second Half of 00");
    foreach (var item in secondHalf)
        Console.WriteLine(item.Id);
}

Console.WriteLine();
Console.WriteLine();

//if you want to split everything into two new dictionaries
var fisrtHalfDict = dict1.ToDictionary(x => x.Key, x => new List<FirstReportsData>(x.Value.Take(x.Value.Count / 2)));
var secondHalfDict = dict1.ToDictionary(x => x.Key, x => new List<FirstReportsData>(x.Value.Skip(x.Value.Count / 2)));

Console.WriteLine("First Half Dict");
foreach (var keyValue in fisrtHalfDict)
{
    Console.WriteLine($"Entries of {keyValue.Key}");
    foreach (var item in keyValue.Value)
        Console.WriteLine(item.Id);
}

Console.WriteLine("Second Half Dict");
foreach (var keyValue in secondHalfDict)
{
    Console.WriteLine($"Entries of {keyValue.Key}");
    foreach (var item in keyValue.Value)
        Console.WriteLine(item.Id);
}

Console.WriteLine();
Console.WriteLine("Read a specific item in the new dicts");
Console.WriteLine();

//get firstHalf of 00
if (fisrtHalfDict.TryGetValue("00", out var firstHalfList))
{
    Console.WriteLine("First Half of 00");
    foreach (var item in firstHalfList)
        Console.WriteLine(item.Id);
}

//get secondHalf of 00
if (secondHalfDict.TryGetValue("00", out var secondHalfList))
{
    Console.WriteLine("Second Half of 00");
    foreach (var item in secondHalfList)
        Console.WriteLine(item.Id);
}

https://dotnetfiddle.net/5Coruu

CodePudding user response:

Dictionary<KeyValuePair<TKey, TValue>> implements IEnumerable<T>, so you can do this with relatively simple LINQ.

var firstHalf = dict1.Where(d => d.Key == "00").Take(dict1.Count / 2);
var secondHalf = dict1.Where(d => d.Key == "00").Except(firstHalf);

CodePudding user response:

Something like this:

List<FirstReportsData> list1 = new List<FirstReportsData>();
List<FirstReportsData> list2 = new List<FirstReportsData>();

foreach(KeyValuePair k in dict1){
    list1.Add(k.Key);
    list2.Add(k.Value);
}

This will loop through your list and add the keys to one list and the values to another list.

  • Related