2

Im trying to write a method which will allow me to search different DataTables, over different columns.

So far i have the following:

string selectedValue;
string searchColumn;
string targetColumn;

        var results = (from a in dt.AsEnumerable()
                       where a.Field<string>(searchColumn) == selectedValue
                      select new
                      {
                        targetColumn = a.Field<string>(targetColumn)
                      }).Distinct();

Which kind of gets the job done, but I'm left with the column name as targetColumn rather than the actual column name I want.

Is there any way to resolve this?

Thanks in advance

CM

CatchingMonkey
  • 1,391
  • 2
  • 14
  • 36
  • I think not, this question is similar to http://stackoverflow.com/questions/6044482/setting-anonymous-type-property-name – Brouwer Mar 16 '12 at 10:46

1 Answers1

1

I make a LINQ to Datatables

public List<DataRow> Where(this DataTable dt, Func<DataRow, bool> pred)
{
    List<DataRow> res = new List<DataRow>();
    try {
        if (dt != null && dt.Rows.Count > 0) {
            for (i = 0; i <= dt.Rows.Count - 1; i++) {
                if (pred(dt(i))) {
                    res.Add(dt(i));
                }
            }
        }
    } catch (Exception ex) {
        PromptMsg(ex);
    }
    return res;
}

Usage :

var RowsList = dt.Where(f => f("SomeField").toString() == "SomeValue" || 
                             f("OtherField") > 5);
Amen Ayach
  • 4,288
  • 1
  • 23
  • 23