Exemple #1
0
        public SelectEnumerator(SelectClause <TResult> selectClause)
        {
            var rowLambda         = selectClause.SelectExpression as LambdaExpression;
            var compiledRowLambda = rowLambda.Compile();
            var paramInfo         = new List <ParameterResultInfo>();

            var generatedSelect = selectClause.Model;

            _selectList = generatedSelect.SelectList;

            foreach (var x in rowLambda.Parameters)
            {
                //look for the type in the select list
                var sel = _selectList.FirstOrDefault(s => s.SourceType == x.Type);
                if (sel == null)
                {
                    paramInfo.Add(null);
                    continue;
                }

                paramInfo.Add(new ParameterResultInfo()
                {
                    Type                 = x.Type,
                    ArgumentMapping      = ConstructorArgumentsMap(x.Type),
                    MemberMapping        = PropertyMapping(x.Type),
                    Constructor          = x.Type.GetConstructors()[0],
                    ConstArgumentsBuffer = new object[x.Type.GetConstructors()[0].GetParameters().Length]
                });
            }

            var connection = VirtualDataSource.GetConnection();

            _result = new List <TResult>();

            connection.Run(selectClause, (reader) =>
            {
                /*
                 *  for each row:
                 *  for each parameter
                 *  1. instantiate parameter
                 *  2. set property values
                 *  3. execute lambda expression
                 *  4. cast to result
                 */

                var arguments = new object[paramInfo.Count];

                for (var i = 0; i < paramInfo.Count; i++)
                {
                    var p = paramInfo[i];
                    if (p != null)
                    {
                        arguments[i] = p.CreateInstance(reader);
                    }
                }
                _result.Add((TResult)compiledRowLambda.DynamicInvoke(arguments));
            });

            listEnumerator = _result.GetEnumerator();
        }
Exemple #2
0
 public static VirtualDataSource GetConnection(string key = null)
 {
     if (_dataSourceInstance == null)
     {
         _dataSourceInstance = new VirtualDataSource(key);
     }
     return(_dataSourceInstance);
 }