ExtensionMethod.NET Home of 876 C#, Visual Basic, F# and Javascript extension methods

DistinctCsv

Returns a distinctive comma-separated list.

Source

/// <summary> Returns a distinctive comma-separated list.</summary> 
public static string DistinctCsv<T>(this IList<T> list)
{
    if (list == null || list.Count == 0)
    {
        return ""; // nothing to work with
    }
    else if (list.Count == 1) 
    {
        return list.ToString(); // no need for distinct or comma joining
    }

    return string.Join(",", list.Distinct()).ToString();
}

Example

private string GetDistinctSetNames()
 {
    List<string> sList = new List<string>();
    foreach (SelectedScenario s in SelectedScenarios) 
    {
       sList.Add(s.Scenario.EvalFactorSet.Name);
    }
             
    return sList.DistinctCsv(); // culls duplicates and makes CSV
}

Author: Eric Bonner Wood

Submitted on: 2 mrt. 2023

Language: csharp

Type: IList<T>

Views: 2435