Linq.Dynamic Select 带有操作的自定义列

Linq.Dynamic Select custom column with operation

我使用 LINQ 动态查询库 https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library 它非常适合查询可选的查询字段

  IQueryable GetSource( DataTable t, String str)
        {
    //Working str = "new (T[\"ss\"] as ss,T[\"xx\"] as xx )"
   //Not Working str = "new (T[\"ss\"] as ss, T[\"xx\"] as xx , T[\"ss\"] +T[\"xx\"] as yy)"
           return (from T in t.AsEnumerable()
                   select new{T}).AsQueryable().Select(str);  
        }

但是当我尝试 (加法-乘法-除法-合并)等操作 出错 如何进行会计操作和合并字段

您正在使用 DataRow item 属性 获取字段的值。此方法 returns 和您无法添加的 object。您需要转换为正确的类型。

"new (T[\"ss\"] as ss, T[\"xx\"] as xx, int(T[\"ss\"])+int(T[\"xx\"]) as yy)"

谢谢 的确,必须转换,因为是对象

 IQueryable GetSource( DataTable t, String str)
        {
    //Working str = "new (T[\"ss\"] as ss,T[\"xx\"] as xx )"
   //Working str = "new (T[\"ss\"] as ss,T[\"xx\"] as xx , string.Concat(T[\"ss\"], T[\"xx\"])  as yy  )"
  //Working str = ""new (T[\"ss\"] as ss,T[\"xx\"] as xx , decimal(T[\"ss\"]) + decimal(T[\"xx\"])  as yy  )""

           return (from T in t.AsEnumerable()
                   select new{T}).AsQueryable().Select(str);  
        }