Return a list of values from a single column in a DataTable I have this code that I was asked to maintain, it is a notification system for a third party application event system (a lot more involved than that but for the sake of this question...)

The Code uses a class DataTable Variable with 4 columns, it takes one of the columns and takes the distinct values and outputs them to a list, but the person that wrote the code made it no so dynamic values are static.

What I am looking for is a way that the underlying DataTable will determine the List&lt;string&gt; instead of having to go through all these if statements that have the wonderful magic strings.

private List&lt;string&gt; GetBaseTypes()
{
    List&lt;string&gt; baseTypes = new List&lt;string&gt;();
    var result = (
       from myRow in _allEventsList.AsEnumerable()
       select myRow.Field&lt;string&gt;("BaseTypeDesc"))
       .Distinct();

    var item = result
       .SingleOrDefault(e =&gt; e == "Order");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Motion");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Service");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Original Filing Document");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Amended Filing Documents");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Case Event Text");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Other Event Types");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Attorney Assignment");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Court Administration");
    if (item != null)
        baseTypes.Add(item);
    item = result
       .SingleOrDefault(e =&gt; e == "Physical Filing Location");
    if (item != null)
        baseTypes.Add(item);

    return baseTypes;
}


Does anyone see something that sticks out where I could simplify this code?

I have been tasked with changing the data going into the DataTable and I don't want to enter in new entries every time something is added to the "BaseTypeDesc" .
Two code-style issues are immediately obvious:


Use braces for 1-liners
only split lines where it overflows


Code like:


var item = result
   .SingleOrDefault(e =&gt; e == "Order");
if (item != null)
    baseTypes.Add(item);
item = result
   .SingleOrDefault(e =&gt; e == "Motion");



Should be

var item = result.SingleOrDefault(e =&gt; e == "Order");
if (item != null)
{
    baseTypes.Add(item);
}
item = result.SingleOrDefault(e =&gt; e == "Motion");


Then, the logical thing to do to simplify those cascading statements, is to create an array of keys to look for (perhaps by loading from a configuration file, or as a static constant array...):

string[] keys = {"Order", "Motion", .....};


then loop through them like:

foreach (string key in keys)
{
    var item = result.SingleOrDefault(e =&gt; e == key);
    if (item != null)
    {
        baseTypes.Add(item);
    }
}