【问题标题】:Ling Query on DataTable with single dynamic Where Clause使用单个动态 Where 子句对 DataTable 进行 Linq 查询
【发布时间】:2016-09-02 14:48:31
【问题描述】:

我正在尝试将此 DataTable Select 查询转换为 Linq。目的是从列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;
    }

这是我迄今为止编写的 Linq 查询。条件已在过滤器中定义。过滤器包含以下类型的值“Project_id = 10”。如何在 where 子句中使用此过滤器。目前它给出了一个错误。 '不能将字符串隐式转换为布尔值'

 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;
    }

最后,我想在行数组中而不是在 EnumerableRowCollection 中获得结果。我想要结果,因为它在上面的数组中。任何帮助都会很有用

【问题讨论】:

标签: c# linq select datatable ienumerable


【解决方案1】:

结合使用 LINQ 和 Datatable.Select 方法:

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();
}

【讨论】:

  • 执行时间相同。我想摆脱这个 datatableLookup.select。
猜你喜欢
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多