0

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.

Rohi_Dev_1.0
  • 372
  • 1
  • 2
  • 19
  • 1
    What is the key? – CodingYoshi May 03 '18 at 11:57
  • 1
    Certainly. Can you show us what you've already tried and where your *specific* issue is? – dymanoid May 03 '18 at 11:58
  • duplicate across all lists or within each list? If the second, Why not have a `Dictionary>` where the key of the inner dictionary is the parameter name? – Zohar Peled May 03 '18 at 11:59
  • Please see the code I have modified it – Rohi_Dev_1.0 May 03 '18 at 12:01
  • `yourCollection.Select(dicItem => { //put code here to check if item exists etc and return anonymous object that has key and Parameter property }).ToDictionary(x => x.AnonymousKey, x => x.Parameter);` – CodingYoshi May 03 '18 at 12:06
  • I don't understand this. Where in your code is the dictionary? If you want to "replace the name with Parametername+ ParameterDetails", then all duplicates in that list get the same combination of Parametername+ ParameterDetails, so they are again duplicates. – Tim Schmelter May 03 '18 at 12:18
  • 1
    "Now I don't want to loop through ParameterDetials List to modify the items" Why not? A loop is the perfect tool to modify items in a collection. Don't use LIN**Q** for this, which should be used only to query something. – Tim Schmelter May 03 '18 at 12:21
  • Hey, I don't understand exactly what you want to solve. I proposed a solution for find duplicates and to get unique values in a List. Can you show more code or be more specific, give example? Where is the dictionary? – CryogenicNeo May 03 '18 at 12:36
  • @TimSchmelter I am looking for better way to do this. – Rohi_Dev_1.0 May 04 '18 at 05:08
  • @CryogenicNeo example added in question – Rohi_Dev_1.0 May 04 '18 at 05:19
  • @Rohi_Dev_1.0 Actually, I do understand what you want to do, but I don't understand which lists store what... I have answered you in almost general. I have used what I understand to try to solve your problem and answer your question. – CryogenicNeo May 04 '18 at 19:55

1 Answers1

0

Yo find they duplicated items in a list you can use LINQ, this is the code using it:

var duplicates = dflist.GroupBy(s => s) .SelectMany(grp => grp.Skip(1));

That code will return all duplicated items in dflist. If you want to select all object that have an unique attribute value, you can use this code line:

var withoutNameDuplicates = dflist.GroupBy(s => s.Name).Select(grp => group.First());

Then you can change the objects Name attribute to the duplicated objects by, using this code:

var nameChangedDuplicates  = duplicates.Select( s => {s.Name = s.Name + "something"; }).ToList();

If you only want to get all the items from the list without duplicates, you can apply Dinstinct() method to dflist, that method will return an IEnumerable<T> result:

var withoutDuplicates = dflist.Distinct();

And, if you want to return a List<T> instead IEnumerable<T>, you must use ToList() method like this:

var withoutDuplicates = dflist.Distinct().ToList();

You can get more information about it in these pages:

c# - How to get duplicate items from a list using LINQ?

Enumerable.DistinctMethod (IEnumerable)

C# - Update all objects in a collection using LINQ

CryogenicNeo
  • 937
  • 12
  • 25