I’m trying to use LINQ to create a new dictionary from an existing one, removing duplicates in the process.
The existing dictionary is as follows:
Dictionary<LoaderConfig, List<ColumnInfo>> InvalidColumns = new Dictionary<LoaderConfig, List<ColumnInfo>>();
public struct LoaderConfig
{
public string ObjectName { get; set; }
public DateTime? LoadDate { get; set; }
public string Load { get; set; }
public string TableName { get; set; }
}
public struct ColumnInfo
{
public string ColumnName { get; set; }
public string DataType { get; set; }
public int DataLength { get; set; }
}
What I want to end up with is a Dictionary<string, List<ColumnInfo>> where the key is the TableName attribute of the LoaderConfig objects and the list of ColumnInfo objects are unique for each TableName.
I started with this based on another post that I found:
var alterations = InvalidColumns
.GroupBy(pair => pair.Key.TableName)
.Select(group => group.First())
.ToDictionary(pair => pair.Key.TableName, pair => pair.Value);
Which doesn’t work because of the First(). I imagine there is a way to acheive this using LINQ extensions, I just need some help finding it.
Thanks!
Personaly I’ll do something like this :
First create a IEqualityComparer for ColumnInfo (used by Distinct)
Then the query :