How to remove blank lines from a list and then remove duplicate values ​​from a list

Suppose I have a list of some column values ​​coming from a table, how to remove empty rows and duplicate values. See the following code:

List<string> dtList = dtReportsList.AsEnumerable().Select(dr => dr.Field<string>("column1")).ToList(); 

This is what I just coded, but Amiram code is more elegant, so I will choose this answer, here is how I did it:

 DataTable dtReportsList = someclass.GetReportsList(); if (dtReportsList.Rows.Count > 0) { List<string> dtList = dtReportsList.AsEnumerable().Select(dr => dr.Field<string>("column1")).ToList(); dtList.RemoveAll(x=>x == ""); dtList = dtList.Distinct().ToList(); rcboModule.DataSource = dtList; rcboModule.DataBind(); rcboModule.Items.Insert(0, new RadComboBoxItem("All", "All")); } 
+45
c # coding-style linq
Aug 08 2018-12-12T00:
source share
3 answers
 dtList = dtList.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList() 

I assumed that the empty string and whitespace are zero. If you cannot use IsNullOrEmpty (allow space) or s != null

+114
Aug 08 2018-12-12T00:
source share

Amiram's answer is correct, but Distinct (), as implemented, is operation N 2 ; for each element in the list, the algorithm compares it with all elements already processed and returns it if it is unique or ignores it if not. We can do better.

A sorted list can be displayed in linear time; if the current element is equal to the previous element, ignore it, otherwise return it. Sorting is NlogN, so even for sorting the collection we get some benefit:

 public static IEnumerable<T> SortAndDedupe<T>(this IEnumerable<T> input) { var toDedupe = input.OrderBy(x=>x); T prev; foreach(var element in toDedupe) { if(element == prev) continue; yield return element; prev = element; } } //Usage dtList = dtList.Where(s => !string.IsNullOrWhitespace(s)).SortAndDedupe().ToList(); 

This returns the same elements; they are just sorted.

+7
Aug 08 2018-12-12T00:
source share

Amiram Korach's solution is really neat. Here's an alternative for the sake of versatility.

 var count = dtList.Count; // Perform a reverse tracking. for (var i = count - 1; i > -1; i--) { if (dtList[i]==string.Empty) dtList.RemoveAt(i); } // Keep only the unique list items. dtList = dtList.Distinct().ToList(); 
+1
Aug 08 2018-12-12T00:
source share



All Articles