Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
To remove the duplicates values in list i used distinct but it is not working

ID Name Questions

1 OLAM "Produto prefer":"Algodão","Plantou algodão":"Sim"
2 OLAM "Produto prefer:"Feijão Buer","Plantou algodão":"Não"
3 OLAM "Produto prefer":"Buer","Plantou algodão":"Jao"


My code as follows
C#
              int columnindex = 43;
              foreach (DataRow row in dtFarmerFarmReports.Rows)
                                {
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(row["farm_detailsdata"].ToString());
                                     string str = string.Empty;

                                     List<string> lst = new List<string>();
                                     lst.AddRange(dict.Keys);
                                     var distinctList = lst.Distinct().ToList();

                                     foreach (var data in distinctList)
                                     {
                                         columnindex++;
                            worksheet.Cells[2, columnindex].Value = data;
                                     }
                                 }


When i run the above code output i get as follows

Produto prefer Plantou algodão Produto prefer Plantou algodão Produto prefer Plantou algodão

But i want the output as follows

Produto prefer Plantou algodão

i want to remove the duplicates.

for that i written a above code but it is not working.

please help me what changes i have to made in my above code to remove the duplicates

What I have tried:

To remove the duplicates values in list i used distinct but it is not working

ID Name Questions

1 OLAM "Produto prefer":"Algodão","Plantou algodão":"Sim"
2 OLAM "Produto prefer:"Feijão Buer","Plantou algodão":"Não"
3 OLAM "Produto prefer":"Buer","Plantou algodão":"Jao"


My code as follows

int columnindex = 43;
foreach (DataRow row in dtFarmerFarmReports.Rows)
{
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(row["farm_detailsdata"].ToString());
string str = string.Empty;

List<string> lst = new List<string>();
lst.AddRange(dict.Keys);
var distinctList = lst.Distinct().ToList();

foreach (var data in distinctList)
{
columnindex++;
worksheet.Cells[2, columnindex].Value = data;
}
}

When i run the above code output i get as follows

Produto prefer Plantou algodão Produto prefer Plantou algodão Produto prefer Plantou algodão

But i want the output as follows

Produto prefer Plantou algodão

i want to remove the duplicates.

for that i written a above code but it is not working.

please help me what changes i have to made in my above code to remove the duplicates
Posted
Updated 9-May-18 1:30am
v2

You are calling Distinct, but you are calling it inside your foreach loop, so you are printing the two keys for every row.

Because you do not want that, you want your Distinct call and the worksheet-related code to be outside your foreach:
C#
int columnindex = 43;

List<string> lst = new List<string>(); // declare "lst" here

foreach (DataRow row in dtFarmerFarmReports.Rows)
{
    var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(row["farm_detailsdata"].ToString());
    string str = string.Empty;
    lst.AddRange(dict.Keys); // add the keys inside your foreach
}

var distinctList = lst.Distinct().ToList(); // perform the Distinct outside the foreach

foreach (var data in distinctList)
{
    columnindex++;
    worksheet.Cells[2, columnindex].Value = data;
}
 
Share this answer
 
The problem is that you're doing it all in the wrong place.
You are reading in your lines of text, then processing each line as JSON - and each line is a separate Dictionary - which you then remove duplicate keys from, and add them to a list and a worksheet.
The problem is that a Dictionary cannot have duplicate keys: every key value must be unique. But each separate line is a different Dictionary - and there is no restriction that prevents the same key in two different Dictionaries.

Read your lines, process each JSON. Add all the keys (without worrying about duplicates) into a single List, and after you have processed all the lines, remove the duplicates and then add everything else to your worksheet.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900