I have a datable wherein column ‘Country’ has duplicate values and can have null values.
I want to get distinct records which allow null values and store it to datatable.
I also want to store into DT variable those that were filtered out.
Hi @ppr sorry for tagging you. I saw your solution in another post regarding similar question as mine but with some conditions. Hope you can help me with this. Thanks alot!
@mnlatam
we can summarize the requirements as following:
group data by Country:
take all group members when Country is empty
take first group member when Country is not empty
get it ordered as by input order
(From d In dtInput.AsEnumerable
Group d By k=d("Country").toString.Trim Into grp=Group
Let gf = grp.Where(Function (x) IsNothing(x("Country")) OrElse String.IsNullOrWhiteSpace(x("Country").toString.Trim)).toList
Let nr = If(gf.Count>0, gf, {grp.First}.toList)
From g In nr
Order By dtInput.Rows.IndexOf(g)
Select g).CopyToDataTable
For the second requirement wherein I need to get the duplicate values except the default, I’m encountering an error for dtOutput2 when I tried to removed the ID column from dtInput. I forgot to mention that my input DT doesn’t have a key ID. Do I need to add temporary primary key for my datatable? Is it because comparing of two DT relies on primary key?
@mnlatam
in general the Except statement relies not a primary key definition
it looks like for any reason the result is empty,which cause this message. Apply following pattern to handle empty result: