如何从列表中删除空字符串,然后从列表中删除重复的值

时间:2021-06-20 07:59:58

Lets say I have a list of some column values coming from a table, how do I remove empty strings and duplicate values. Please see the following code:

假设我有一个来自表的列值列表,如何删除空字符串和重复值。请参阅以下代码:

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

This is what I have coded just now but but Amiram's code is way more elegant, so I will choose that answer here is how I did it:

这就是我刚刚编写的代码,但是Amiram的代码更优雅,所以我会选择这个答案就是我这样做的:

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"));


       }

3 个解决方案

#1


138  

dtList  = dtList.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList()

I assumed empty string and whitespace are like null. If not you can use IsNullOrEmpty (allow whitespace), or s != null

我假设空字符串和空格就像null。如果不是,您可以使用IsNullOrEmpty(允许空格)或s!= null

#2


7  

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

Amiram的答案是正确的,但实施的Distinct()是N2操作;对于列表中的每个项目,算法将其与所有已处理的元素进行比较,如果它是唯一的则返回它,否则忽略它。我们可以做得更好。

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

排序列表可以在线性时间内重复;如果当前元素等于前一个元素,则忽略它,否则返回它。排序是NlogN,所以即使必须对集合进行排序,我们也会获得一些好处:

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're just sorted.

这返回相同的元素;他们只是排序。

#3


1  

Amiram Korach solution is indeed tidy. Here's an alternative for the sake of versatility.

Amiram Korach解决方案确实很整洁。为了多功能性,这里有一个替代品。

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


138  

dtList  = dtList.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList()

I assumed empty string and whitespace are like null. If not you can use IsNullOrEmpty (allow whitespace), or s != null

我假设空字符串和空格就像null。如果不是,您可以使用IsNullOrEmpty(允许空格)或s!= null

#2


7  

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

Amiram的答案是正确的,但实施的Distinct()是N2操作;对于列表中的每个项目,算法将其与所有已处理的元素进行比较,如果它是唯一的则返回它,否则忽略它。我们可以做得更好。

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

排序列表可以在线性时间内重复;如果当前元素等于前一个元素,则忽略它,否则返回它。排序是NlogN,所以即使必须对集合进行排序,我们也会获得一些好处:

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're just sorted.

这返回相同的元素;他们只是排序。

#3


1  

Amiram Korach solution is indeed tidy. Here's an alternative for the sake of versatility.

Amiram Korach解决方案确实很整洁。为了多功能性,这里有一个替代品。

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();