using System; using System.Collections.Generic; using System.Linq; using System.Collections; namespace Marr.Data.QGen { /// /// This class holds a collection of Table objects. /// public class TableCollection : IEnumerable { private List
_tables; public TableCollection() { _tables = new List
(); } public void Add(Table table) { if (this.Any(t => t.EntityType == table.EntityType)) { // Already exists -- don't add return; } // Create an alias (ex: "t0", "t1", "t2", etc...) table.Alias = string.Format("t{0}", _tables.Count); _tables.Add(table); } public void ReplaceBaseTable(View view) { _tables.RemoveAt(0); Add(view); } /// /// Tries to find a table for a given member. /// public Table FindTable(Type declaringType) { return EnumerateViewsAndTables().Where(t => t.EntityType == declaringType).FirstOrDefault(); } public Table this[int index] { get { return _tables[index]; } } public int Count { get { return _tables.Count; } } /// /// Recursively enumerates through all tables, including tables embedded in views. /// /// public IEnumerable
EnumerateViewsAndTables() { foreach (Table table in _tables) { if (table is View) { foreach (Table viewTable in (table as View)) { yield return viewTable; } } else { yield return table; } } } public IEnumerator
GetEnumerator() { return _tables.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return _tables.GetEnumerator(); } } }