Ejemplo n.º 1
0
        /// <summary>
        /// If no parameters are passed in, this method instructs the DataMapper to load all related entities in the graph.
        /// If specific entities are passed in, only these relationships will be loaded.
        /// </summary>
        /// <param name="childrenToLoad">A list of related child entites to load (passed in as properties / lambda expressions).</param>
        public virtual QueryBuilder <T> Graph(params Expression <Func <T, object> >[] childrenToLoad)
        {
            TableCollection tablesInView = new TableCollection();

            if (childrenToLoad.Length > 0)
            {
                // Add base table
                tablesInView.Add(_tables[0]);

                foreach (var exp in childrenToLoad)
                {
                    MemberInfo child = (exp.Body as MemberExpression).Member;

                    var node = EntGraph.Where(g => g.Member != null && g.Member.EqualsMember(child)).FirstOrDefault();
                    if (node != null)
                    {
                        tablesInView.Add(new Table(node.EntityType, JoinType.None));
                    }

                    if (!_childrenToLoad.ContainsMember(child))
                    {
                        _childrenToLoad.Add(child);
                    }
                }
            }
            else
            {
                // Add all tables in the graph
                foreach (var node in EntGraph)
                {
                    tablesInView.Add(new Table(node.EntityType, JoinType.None));
                }
            }

            // Replace the base table with a view with tables
            View view = new View(_tables[0].Name, tablesInView.ToArray());

            _tables.ReplaceBaseTable(view);

            _isGraph = true;
            return(this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Overrides the base view name that will be used in the query.
        /// Will try to use the mapped "AltName" values when loading the columns.
        /// </summary>
        public virtual QueryBuilder <T> FromView(string viewName)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                throw new ArgumentNullException("view");
            }

            _isFromView = true;

            // Replace the base table with a view with tables
            if (_tables[0] is View)
            {
                (_tables[0] as View).Name = viewName;
            }
            else
            {
                View view = new View(viewName, _tables.ToArray());
                _tables.ReplaceBaseTable(view);
            }

            return(this);
        }