动态Linq T选择对象[]

时间:2016-08-11 11:00:23

标签: c# linq dynamic

我正在尝试为对象数组(object [])创建选择通用对象(T)的函数

public Expression<Func<T, object[]>> NewObject<T>(CacheLinqExpresionItem[] columns)
{
    Type returnType = typeof(object[]);
    NewExpression ctor = Expression.New(returnType.GetConstructor(new Type[] { typeof(int) }), Expression.Constant(columns.Length));
    ParameterExpression resultVariable = Expression.Variable(returnType, "r");

    int x = 0;
    List<Expression> codeLines = new List<Expression>();
    foreach (CacheLinqExpresionItem col in columns)
    {
        //IndexExpression getColumn = Expression.ArrayAccess(ctor, Expression.Constant(x));
        //var propertyExpression = Expression.Property(resultVariable, col.PropertyInfo);
        //var cast = Expression.Convert(propertyExpression, typeof(object));
        UnaryExpression cast = Expression.Convert(col.Value, typeof(object));

        codeLines.Add(cast);
        //codeLines.Add(Expression.Assign(getColumn, cast));
        x++;
    }

    BlockExpression codeBlock = Expression.Block(codeLines);
    return Expression.Lambda<Func<T, object[]>>(codeBlock, resultVariable);
}

但我不能,我的意图是生成更优的json

当我序列化我的对象时,这是输出

{
  "Result": "Ok",
  "Data": {
    "Items": [
      {
        "Id": 1,
        "Name": "Fernando",
        "Email": "xxx@gmail.com"
      },
      {
        "Id": 2,
        "Name": "Pedro",
        "Email": "xxx@gmail.com"
      }
    ],
    "Page": {
      "Current": 1,
      "PeerPage": 100
    }
  }
}

我假装不要重复这些名字......

{
      "Result": "Ok",
      "Data": {
        "Columns": [
          {
            "Name": "Id",
            "Type": "Int64"
          },
          {
            "Name": "Name",
            "Type": "String"
          },
          {
            "Name": "Email",
            "Type": "String"
          }
        ],
        "Items": 
        [
          [ 1,"Fernando","xxx@gmail.com" ],
          [ 2,"Pedro"   ,"xxx@gmail.com" ],
          [ 3,"Juan"    ,"xxx@gmail.com" ]
        ],
        "Page": {
          "Current": 1,
          "PeerPage": 100
        }
      }
    }

问题是,谁可以将Linq Select的TEntity转换为对象数组(变量列)?

我的失败是数组初始化......

像这样使用

string[] words = string.IsNullOrEmpty(query) ? null :
                query.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

CacheLinqExpresionItem[] cols;
IQueryable<TEntity> q = Cache.WhereForSearch<TEntity>(Context.Set<TEntity>().AsNoTracking(), words)
    .Skip((page.Page - 1) * page.PeerPage)
    .Take(page.PeerPage);

object[] ret;
switch (type)
{
    case ECrudListType.Optimized:
        {
            string sc = Cache.Listables.ValidateColumns(columns, out cols);

            ret = q
              .Select(Cache.NewObject<TEntity>(cols))
              .ToArray();
            break;
        }
    case ECrudListType.Regular:
        {
            ret = q
              .Select(Cache.Listables.ValidateColumns(columns, out cols))
              .ToDynamicArray();
            break;
        }
    default: return new ResultError(1, Localizer["invalid_parameters"]);
}

和CacheLinqExpresionItem.Value是PropertyName(T)的表达式

0 个答案:

没有答案