I have List<ParametersDetails>
. Parameters and ParametersDetails class is like -
public class ParameterDetails
{
public string Name { get; set; }
public List<Parameter> Parameters{get;set;}
}
public class Parameter
{
public string Name { get; set; }
public string Type { get; set; }
public string Value { get; set; }
public string AccountName { get; set; }
}
I want ParameterDetails list should not contain any parameter with duplicate name. If any duplicate parameter name found I want to replace the name with Parametername+ parameterDetails name from the dictionary. I can do it by traversing items and then modify items but I want to do it with lesser code. The problem is how to traverse and find duplicates from list ..? Is it possible in Linq?
What I am doing right now - I have taken all the parameters in 1 list and find out duplicates
var hasDupes = dflist.GroupBy(x => new { x.Name })
.Where(x => x.Skip(1).Any()).ToArray();
Next, I am selecting the item from List
parameterdetails.Select(x => x.Parameters.Where(p => p.Name == dupeList.Key.ToString())).ToList();
Now I don't want to loop through ParameterDetials List to modify the items. Is any easier way?
Ex- I am having 2 items in ParameterDetails like -
ParameterDetails:
[
{
name: "test1",
Parameters:[
{
"Name":"param1",
"Type":"paramtype1",
"Value":"value1",
"AccountName":"accname1"
},
{
"Name":"param2",
"Type":"paramtype2",
"Value":"value2",
"AccountName":"accname2"
}]
},
{
name: "test2",
Parameters:[
{
"Name":"param1",
"Type":"paramtype11",
"Value":"value11",
"AccountName":"accname11"
},
{
"Name":"param2",
"Type":"paramtype22",
"Value":"value22",
"AccountName":"accname22"
}]
}]
If I am having param1 as a duplicate name so in that I want to replace it as "param1+test2" so that it will be unique.