0

I am trying to Convert this DataTable Select query to Linq. The purpose is to get the values from column lookupValueField

private object[] GetValueFromLookup(MultipleKeyConditionBuilder filter, string lookupValueField, DataTable datatableLookup)

   { 
        object[] result;
        DataRow[] rows = datatableLookup.Select(filter.Condition);
        result = new object[rows.Count()];
        for (int i = 0; i < rows.Count(); i++)
        { 
            result[i] = rows[0][lookupValueField];
        }

        return result;
    }

This is the Linq query I have written so Far. The condition is already defined in the filter. filter contains following type of value 'Project_id = 10'. How can I use this filter in my where clause. currently it is giving an error. 'cannot implicitly convert string to bool'

 private object[] GetValueFromLookup(MultipleKeyConditionBuilder filter, string lookupValueField, DataTable datatableLookup)
    {
        object[] result;

        EnumerableRowCollection<DataRow> rows;
        //DataRow[] rows;
        rows = from myRow in datatableLookup.AsEnumerable()
                      where filter.Condition  
                      select myRow ;


        result = new object[rows.Count()];
        for (int i = 0; i < rows.Count(); i++)
        {
            result[i] = rows; // should give only lookupcolumn 
            //result[i] = rows[0][lookupValueField];
        }

        return result;
    }

lastly I want to get result in an array of rows and not in EnumerableRowCollection. I want the result as it is in above array. Any help would be useful

مسعود
  • 679
  • 10
  • 25
  • This question has been asked before, please see [How to use a string variable in the linq where clause](http://stackoverflow.com/questions/31342172/how-to-use-a-string-variable-in-the-linq-where-clause). You cannot use a string in the LINQ where clause. – sly Sep 02 '16 at 16:24
  • Thanks for the help – مسعود Sep 05 '16 at 07:22

1 Answers1

0

Using a combination of LINQ and the Datatable.Select method:

private object[] GetValueFromLookup(MultipleKeyConditionBuilder filter, string lookupValueField, DataTable datatableLookup)
{
    IEnumerable<Object> query =
        from rows in datatableLookup.Select(filter.Condition)
        select rows.Field<Object>(lookupValueField);

    return query.ToArray();
}
sly
  • 300
  • 1
  • 5