none
Loop through all tables in a DataContext RRS feed

  • Question

  • Hi all.

     

    My question is, how can I loop through all tables o a DC? I can find the MetaTables by calling dc.Mapping.GetTables(), but how can I access the real dc-tables? IF there would have been a Tables property on the DC and a Table type that didn't need the generic parameter then this is what I'd like to do:

     

    foreach(Table t in dc.Tables)

            t.Refresh(...);

     

    Regards,

    /Jonas

     

     

     

     

    Wednesday, June 18, 2008 2:20 PM

Answers

  • There is a GetTable method on the DC.  Once you have the metaTable, you can presumably get the corresponding .NET Type from the metaTable (metaTable.RowType.Type ?).  So, get all the metatables; loop through the list of metaTables, for each one, get the corresponding .NET Type and call GetTable.  Will that do what you want?

     

    John

    Friday, June 20, 2008 12:19 AM

All replies

  • If your ultimate objective is to refresh all entities, then I'd recommend you create a new DataContext instance and re-issue the query.

     

    Thanks,

     

    --Samir

     

     

    Wednesday, June 18, 2008 5:07 PM
  • Even though it is not the recommended practice, we use the DataContext as a cached singleton object since it suits the solution and we have not yet stumbled on any obstacles that have led us to re-evaluate this strategy. So, even though I understand your advise, it is not applicable in this case.

     

    So, the question still stands, is there a way to do it?

     

    Regards,

    /Jonas

    Wednesday, June 18, 2008 5:55 PM
  • I would recommend you use reflection to obtain the collection of all your data context's properties with the Table<> type and iterate through this collection.

     

    While I haven't tested it yet, I think the following (extension method) implementation could work:

     

    Code Snippet

    public IEnumerable<ITable> GetTables(this DataContext context)

    {

    return context.GetType().GetProperties()

    .Where(propertyInfo => propertyInfo.PropertyType == typeof(Table<>))

    .Select(propertyInfo => propertyInfo.GetValue(context, null) as ITable);

    }

     

    Then, you can simply call this method as part of your refresh loop.

    Thursday, June 19, 2008 6:28 PM
  • There is a GetTable method on the DC.  Once you have the metaTable, you can presumably get the corresponding .NET Type from the metaTable (metaTable.RowType.Type ?).  So, get all the metatables; loop through the list of metaTables, for each one, get the corresponding .NET Type and call GetTable.  Will that do what you want?

     

    John

    Friday, June 20, 2008 12:19 AM
  • Hi,
    After reading this post, i was tempted to try something.  I didnt want to copy and paste Add, Delete, Update, List methods.  I have an abstract class ParentClass<T> and all my entity classes inherit from this class, ParentClass<Organization>.  Now I have the add method in place but how can get the ToList() to work?  ToList is extension on IEnumerable<> so how would it work?

    public

     

    static T Add(T pType)
    {
    using (OrgstructureDataContext context = new OrgstructureDataContext())
    {
    ITable gTable = context.GetTable(typeof(T));
    gTable.InsertOnSubmit(pType);
    context.SubmitChanges()
    }
    return pType;
    }

    Your advice and help is appreciated.

    Thanks


    kashif
    Monday, August 17, 2009 7:04 AM
  • There is a GetTable method on the DC.  Once you have the metaTable, you can presumably get the corresponding .NET Type from the metaTable (metaTable.RowType.Type ?).  So, get all the metatables; loop through the list of metaTables, for each one, get the corresponding .NET Type and call GetTable.  Will that do what you want?

     

    John

    can i iterate this datacontext by using for loop?
    :)
    Monday, September 12, 2011 9:11 AM