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

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

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

这是我刚才编写的代码,但是阿米兰姆的代码更加优雅,所以我会选择这个答案:

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"));
}
129292 次浏览
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

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.

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:

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.

Amiram Korach solution is indeed tidy. 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();

To simplify Amiram Korach's solution:

dtList.RemoveAll(s => string.IsNullOrWhiteSpace(s))

No need to use Distinct() or ToList()