score:2
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.)
Source: stackoverflow.com
Related Query
- Searching in two collections
- Check for any element that exists in two collections
- Quickest way to find the complement of two collections in C#
- How to concatenate two collections by index in LINQ
- Any Intersection in Two Collections
- I need to compare two very large collections with potentially missing elements
- How to intersect two different IEnumerable collections
- Intersect two collections which contain different types
- LINQ Source Code Available
- Comparing two collections with IEquatable while using only LINQ
- C# Linq, Searching for same items in two lists
- How can I combine this code into one or two LINQ queries?
- Difference between the returned values of two selector functions in this code
- Compare two collections and add/remove from one to make them match
- .NET 4 Code Contracts: "requires unproven: source != null"
- Determine if two collections share at least one element
- Linq to compare two collections in C#
- Is there a way to speed up this code that finds data changes in two XML files?
- How to use LINQ to combine two or more collections into one collection
- How to Union two collections efficiently?
- How to Join two dictionary collections linq query
- Union two collections but only for objects with a matching id
- creating Linq to sqlite dbml from DbLinq source code
- Can we compare two complex collections field by field using IEqualityComparer interface using LINQ extension method SequenceEqual
- Create a parent -> children hierarchy from two collections
- LINQ code to combine two database tables?
- joining two collections in C# or JavaScript
- Comparing two collections
- How to Compare two IEnumerable Collections using LINQ
- C# : How to compare two collections (System.Collection.Generic.List<T>) using Linq/Lambda?
More Query from same tag
- Using LINQ to concatenate strings
- Split collections of interfaces implementing a common interface by interface, not by implementations
- How to make a linq Sum return null if the summed values are all null
- How to sort a collection based on a subcollection property
- using a string in a linq where comparison
- ObservableCollection projections that is also observable
- Turn SQL query which contains multiple join, left join, group by and having to EF core
- Abstract sequence processing that involves state
- Retrieve single record to model with EF Linq, does it need to loop to populate?
- Create nodes based on id values of other nodes
- Linq query return coumn value null handle
- C# Dataset column removal using LINQ
- Search by using EntityFramework
- Running a simple LINQ query in parallel
- In LINQ, how can I do an .OrderBy() on data that came from my .Include()?
- Retrieve data from many to many relationship in asp.net mvc linq
- using Linq to sort a list based on the sum of values in each item
- C# retrieve multiple XML attributes from specific elements for manipulation
- Help convert SQL statement to LINQ Lambda (need help with group by and sum)
- Return @@RowCount in LINQ to SQL INSERT
- Linq to XML, C#
- Parse and extract a value from streaming XML file?
- Asserting Elements Parsed from XML
- Linq Where filtration
- Why doesn't LINQ include a `distinct` keyword?
- LINQ Query for stock and value by FIFO Method
- Can I use linq to remove items from asp .net DropDownList.Items?
- How to convert List<Company> to List<ICompany>
- Select with list among the variables
- How to generate an array from multiples properties of a class