-1

I am building a table on the fly in C#. This table may or may not contain certain columns. I need a row made up of the distinct values from a raw table. (i.e. Agent Name, Manager Name etc).

I use:

var DistinctTable = SourceTable.DefaultView.ToTable(true, "AName", "MName");

and it works fine, but I need this to work with something like

    string Groupby = "";

    if (AName != "")
    {
        Groupby = AName;
    }
    if (MName != "")
    {
        Groupby = Groupby + MName;
    }

var DistinctTable = SourceTable.DefaultView.ToTable(true, Groupby);

I'm dumbing this down a bit for simplicity but the premise is there. I have tried options of adding in quotes, adding in comma's etc. Nothing works the best I have done is to receive the error

There is no column '"AName","MName"' in table (x)

is this possible?

2 Answers2

0

As DistinctTable is being made (back?) into a DataTable you can therefore do this:

if (DistinctTable.Columns.Contains("ColumnName"))
{
    //[logic]
}

Edited to add:

DataTable DistinctTable = SourceTable.DefaultView.ToTable();
if (DistinctTable.Rows.Count > 0)
{
    if (!DistinctTable.Columns.Contains("ColumnA"))
    {
        DistinctTable.Columns.Add("ColumnA", typeof(String));
    }
    if (!DistinctTable.Columns.Contains("ColumnB"))
    {
        DistinctTable.Columns.Add("ColumnA", typeof(String));
    }
    if (!DistinctTable.Columns.Contains("ColumnC"))
    {
        DistinctTable.Columns.Add("ColumnC", typeof(String));
        //you can also move the new columns by using SetOrdinal, like so:
        DistinctTable.Columns[DistinctTable.Columns.Count - 1].SetOrdinal(DistinctTable.Columns.Count - 2);
        //but careful, this gets messy very quickly
    }
}
//return DistinctTable //(?)
Hugo Yates
  • 2,081
  • 2
  • 26
  • 24
  • I potentially need multiple columns to be distinct. How can I start making (back) into a DataTable if I haven't finished the request? – BigDaddyKo Mar 19 '15 at 16:08
  • `ToTable()` converts your `DataView` to a `DataTable`. You can always add columns into the DataTable by doing `DistinctTable.Columns.Add("AName", typeof(String));` You can use that in conjunction with `Contains` to ensure you only add the column when necessary. – Hugo Yates Mar 19 '15 at 16:14
  • Updated answer for a better example/clarification – Hugo Yates Mar 19 '15 at 16:26
0

The second parameter of DefaultView.ToTable is params string[] so you should be able to create an array to pass into it.

List<string> groupBys = new List<string>();

if (AName != "")
{
    groupBys.Add(AName);
}
if (MName != "")
{
    groupBys.Add(MName);
}

var DistinctTable = SourceTable.DefaultView.ToTable(true, groupBys.ToArray());
juharr
  • 31,741
  • 4
  • 58
  • 93
  • A great Idea and I attempted this but I get the same error about the column not being in the table. – BigDaddyKo Mar 19 '15 at 16:09
  • My Mistake. Thank you VERY Much Juharr. This worked!! I was working on this very late last night and I truly thought I had tried a string. – BigDaddyKo Mar 19 '15 at 16:22