Remove duplicate records from List in C#
Here comes the answer of a simple question
How to remove duplicate records from List in C#?
and here is simple answer
public List selectColumns()
{
conn= new OracleDB(Credentials.userName, Credentials.password, Credentials.dataSource, Credentials.host);
var query = "select COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='PRODUCTSALE'";
List columnsList= new List();
var result = conn.executeSelect(query);
if (result != null && result.Rows.Count > 0)
{
foreach (DataRow dr in result.Rows)
{
var singleColumn= new ColumnName();
singleColumn.ColumnNames = dr.Field("COLUMN_NAME");
columnsList.Add(singleColumn);
}
}
//here we use LINQ to remove duplicate recordes from List
columnsList = columnsList.GroupBy(i => i.ColumnNames)
.Select(g => g.First()).ToList();
return columnsList;
}
How to remove duplicate records from List in C#?
and here is simple answer
public List
{
conn= new OracleDB(Credentials.userName, Credentials.password, Credentials.dataSource, Credentials.host);
var query = "select COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='PRODUCTSALE'";
List
var result = conn.executeSelect(query);
if (result != null && result.Rows.Count > 0)
{
foreach (DataRow dr in result.Rows)
{
var singleColumn= new ColumnName();
singleColumn.ColumnNames = dr.Field
columnsList.Add(singleColumn);
}
}
//here we use LINQ to remove duplicate recordes from List
columnsList = columnsList.GroupBy(i => i.ColumnNames)
.Select(g => g.First()).ToList();
return columnsList;
}
No comments: