Accepted answer

It seems like you're trying to associate a BindingSource to the first DataTable with the same Type name. The straightforward approach would be to prepare a Dictionary up front that maps the names you're using for the lookup to the tables:

var tablesByName = (from set in model.DataSource 
                    from table in set.Tables 
                    select table) // get all tables of all sets
                   .ToDictionary(table => (table.GetType() as Type).Name)

(If the tables never change, this can be a static field that's only computed once.)

Then the rest of your code should reduce to:

foreach (var source in view.DataContext) 
    DataTable table;
    var name = (source.DataSource as Type).Name;
    if (tablesByName.TryGetValue(name, out table))
        source.DataSource = table;

I've omitted handling things like there being tables with duplicate names. (Which would make ToDictionary() fail, so you have to get rid of duplicates before that call.)

Related Articles