tags:

views:

22

answers:

1

Hi,

How can insert an item to Expression array? For example: some code like

Expression<Func<int, bool>>[] exprs;
Expression<Func<int, bool>> expr = i => i > 0;
exprs.Add(expr);

Thanks

+2  A: 

If you want to use an array, you need to initialize it first:

Expression<Func<int, bool>>[] exprs = new Expression<Func<int, bool>>[arrayLength];
Expression<Func<int, bool>> expr = i => i > 0;
exprs[0] = expr;

This just just like any other array type in C#. For details on arrays, see MSDN.

If you just need a collection that can grow as needed, consider List<T> instead:

List<Expression<Func<int, bool>>> exprs = new List<Expression<Func<int, bool>>>();
Expression<Func<int, bool>> expr = i => i > 0;
exprs.Add(expr);  // This works with List<T> - you don't need the size in advance.
Reed Copsey
Basicly I'm trying to create an array of Expression<Fun<int, bool>> at runtime, and use it for Expression.Call(). I would like use Dynamic Linq Library now rather than write it from scratch. Thanks for your help.
Zalan