LINQ query failing when we we have duplicate values in dictionary

Hi,
I have implemented VLookup using LINQ. I assigned it to dictionary. but sometimes my columns data is having duplicate values. at that time is is giving me error. how to modify the code so that it accepts the duplicate values also

This is the LINQ Query i have given in assign block

(From i In Enumerable.Range(0, dt1.Rows.Count)
Group Join d2 In dt2.AsEnumerable
On dt1.Rows(i)(“Transaction Confirm No”).toString.Trim Equals d2(“Document Header Text”).toString.Trim Into grp=Group
From g In grp.DefaultIfEmpty
Let lkv = If(isNothing(g),“N/A”,g(“Material Document”).toString)
Select t=Tuple.Create(i, lkv)).ToDictionary(Function (t) t.Item1, Function (t) t.Item2)

image

1 Like

Hi @Sirisha_Siri ,

I would suggest splitting your operations into steps, so that you will be in a better position to debug them.
I think you might already know why you are facing this issue, but even then try splitting removing the part where you are converting it to a Dictionary and have a look at it as a DataTable or an Array(string):

(From i In Enumerable.Range(0, dt1.Rows.Count)
Group Join d2 In dt2.AsEnumerable
On dt1.Rows(i)(“Transaction Confirm No”).toString.Trim Equals d2(“Document Header Text”).toString.Trim Into grp=Group
From g In grp.DefaultIfEmpty
Let lkv = If(isNothing(g),“N/A”,g(“Material Document”).toString)
Select {i, ikv}).ToArray()

Look it up in the Immediate Panel or export it to a text file, and try to figure out what steps have to be made.

Kind Regards,
Ashwin A.K

Hi @ashwin.ashok

While removing duplicates, how to remove the first duplicate one and keep the second duplicate one. Here in the below code, i am able to keep first duplicates but not the second one…could u pls help me to modify it to keep
second duplicates
Main.xaml (8.1 KB)
MKPFOutput1_4_2022.xlsx (104.2 KB)

Hi @Sirisha_Siri ,

I’m not sure if this is the same issue, or if its a different one, but if you want to retain only the last item from a set of duplicates, then you can use this →

(From d In dtData.AsEnumerable
Group d By k=d("GUID").toString.Trim Into grp=Group
Where grp.Count >1
Select grp.Last()).CopyToDatatable

However, if you want to retain even the unique values, and the last value from the duplicate set, then you can use this →

(From d In dtData.AsEnumerable
Group d By k=d("GUID").toString.Trim Into grp=Group
Select grp.Last()).CopyToDatatable

Kind Regards,
Ashwin A.K

Thanks, @ashwin.ashok

I want to retain even the unique values, and the last value from the duplicate set. I modified the code, It helped

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.