HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Return a list of values from a single column in a DataTable

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
columnreturndatatablesinglevalueslistfrom

Problem

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 instead of having to go through all these if statements that have the wonderful magic strings.

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

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

Solution

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 => e == "Order");
if (item != null)
    baseTypes.Add(item);
item = result
   .SingleOrDefault(e => e == "Motion");


Should be

var item = result.SingleOrDefault(e => e == "Order");
if (item != null)
{
    baseTypes.Add(item);
}
item = result.SingleOrDefault(e => 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 => e == key);
    if (item != null)
    {
        baseTypes.Add(item);
    }
}

Code Snippets

var item = result
   .SingleOrDefault(e => e == "Order");
if (item != null)
    baseTypes.Add(item);
item = result
   .SingleOrDefault(e => e == "Motion");
var item = result.SingleOrDefault(e => e == "Order");
if (item != null)
{
    baseTypes.Add(item);
}
item = result.SingleOrDefault(e => e == "Motion");
string[] keys = {"Order", "Motion", .....};
foreach (string key in keys)
{
    var item = result.SingleOrDefault(e => e == key);
    if (item != null)
    {
        baseTypes.Add(item);
    }
}

Context

StackExchange Code Review Q#72034, answer score: 5

Revisions (0)

No revisions yet.