score:2
Accepted answer
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.data;
namespace consoleapplication1
{
class program
{
static void main(string[] args)
{
datatable dt = new datatable();
dt.columns.add("id", typeof(int));
dt.columns.add("name", typeof(string));
dt.columns.add("dept", typeof(string));
dt.rows.add(1, "test1", "sample1");
dt.rows.add(2, "test2", "sample2");
dt.rows.add(3, "test3", "sample3");
dt.rows.add(4, "test4", "sample4"); // duplicate
dt.rows.add(5, "test4", "sample4"); // duplicate
dt.rows.add(6, "test4", "sample4"); // duplicate
dt.rows.add(7, "test4", "sample5");
datatable dt2 = dt.asenumerable()
.orderby(x => x.field<int>("id"))
.groupby(x => new { name = x.field<string>("name"), dept = x.field<string>("dept") })
.select(x => x.first())
.copytodatatable();
}
}
}
score:2
static datatable removeduplicatesfromdatatable(datatable table, list<string> keycolumns)
{
dictionary<string, string> uniquenessdict = new dictionary<string, string>(table.rows.count);
stringbuilder stringbuilder = null;
int rowindex = 0;
datarow row;
datarowcollection rows = table.rows;
while (rowindex < rows.count - 1)
{
row = rows[rowindex];
stringbuilder = new stringbuilder();
foreach (string colname in keycolumns)
{
//stringbuilder.append(((double)row[colname]));
stringbuilder.append(row[colname]);
}
if (uniquenessdict.containskey(stringbuilder.tostring()))
{
rows.remove(row);
}
else
{
uniquenessdict.add(stringbuilder.tostring(), string.empty);
rowindex++;
}
}
return table;
}
Source: stackoverflow.com
Related Query
- Delete duplicate rows from datatable based on 2 columns in c#
- LINQ to remove duplicate rows from a datatable based on the value of a specific row
- Get duplicate rows from datatable based on single column
- How to delete rows from DataTable with LINQ?
- How to select rows from DataTable based on Index / Row Number?
- howto delete rows from DataTable in C# with a filter?
- Remove duplicate rows of a datatable based on a list of key fields
- error when tried to delete empty rows from datatable
- What's The Best Way To Remove Duplicate Rows Based On A Row Item In A DataTable With VB.Net?
- using LINQ to delete specific rows from DataTable
- Delete Columns from datatable
- Delete Older Duplicate record from Datatable C#
- Find and replace/update specific entries from all rows of selected columns in DataTable
- Return Rows with Unique and Duplicate values from dataTable using LINQ
- Remove duplicate rows from a list based on selected columns?
- Select distinct rows from datatable in Linq
- Remove duplicates from list based on multiple fields or columns
- How to find duplicate record using Linq from DataTable
- Remove Duplicate item from list based on condition
- How to group by on multiple columns from datatable with linq?
- Create a sequence of anonymous types from the rows in a DataTable
- get distinct rows from datatable using Linq (distinct with mulitiple columns)
- DataTable select with LiNQ and check is there duplicate rows or not
- How to get 2 columns from datatable in linq
- How to delete multiple rows using LINQ in linq2db Templates based on two tables?
- Remove rows from datatable matching a List<string>
- Selecting distinct rows from datatable in Linq
- C# - Remove rows with the same column value from a DataTable
- Remove columns from datatable
- Finding common columns from two datatable and using those for Join condition in LINQ
More Query from same tag
- where can I find a good example of using linq & lambda expressions to generate dynamic where and orderby sql?
- Passing a method to a LINQ query
- Entity Framework - Most recent record in the left join
- Paging with LINQ without sorting
- WCF Data Services Expand as Linq Statement
- Nested include Entity Framework Core
- Error when using Contains within linq query with results from another query
- Linq to remove string from list
- linq search functionality skip where clause if null?
- Dynamic Linq GroupBy Select not working properly
- EntityFramework Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context
- ObservableCollection not updated when doing a second LINQ query?
- ASP.Net MVC ViewData Issue
- IEnumerable extension to pull results in batches
- LINQ To XML reading child element values filtered on attribute value
- Translating query from SQL to EF Lambda expressions
- Foreign keys which does not exist
- 2sxc Linq query for Categories
- Error - Corresponding column type must be compatible - DateTime field - Entity Framework, Informix, Load multiple childs
- Linq: Group by one column but must distinct other column
- "the method join is not supported" with Tridion OData service & Linq
- Select at runtime the field in a table with LINQ to SQL
- C# Linq, Searching for same items in two lists
- C# Dictionary.Add is forgotten when it gets out of the loop
- LINQ to SQL group by with take
- C# MongoDb driver include child object
- Reading double Numbers from a text file which contains string and number mixed
- how can I see the inputs to a LINQ query?
- Entity Framework Core : LINQ advise needed on better approach using include for relational tables
- Creating Dynamic Fetch Expression