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 Articles
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- Searching in two collections
- creating Linq to sqlite dbml from DbLinq source code
- Linq2Sql -> Searching the database against a local collection of values - "Queries with local collections are not supported"
- Code First EF: While searching database table is it possible to retrieve list of items it has in DataModel?
- source code for LINQ 101 samples
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Collections manipulation, need help optimizing this code from a report generator
- Convert string[] to int[] in one line of code using LINQ
- Searching if value exists in a list of objects using Linq
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Elegant way to combine multiple collections of elements?
- LINQ Ring: Any() vs Contains() for Huge Collections
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- Searching a tree using LINQ
- How to use LINQ with dynamic collections
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- linq question: querying nested collections
- How to merge a collection of collections in Linq
- Check for any element that exists in two collections
- What does this C# code with an "arrow" mean and how is it called?
- Round double in LINQ to Entities
- LINQ-to-Entities get rid of sp_executesql
- Search using linq
- Select across associative tables
- Check if a specific value appears more than once in a table with a LINQ query
- How to use LINQ in C# to query windows local user accounts
- Linq to SQL
- Linq filter parent objects based on child objects collection
- A lambda expression with a statement body cannot be converted to an expression tree in nopCommerce
- Does LINQ perform subsequent Selects in memory?
- linq where count of collection greater than
- Linq include with where clause
- LINQ to Entities .contains is ignoring results with NULL
- C# Explanation about a lambda expression
- Subscribing to a event in a closure while iterating through a collection results in the last item subscribing to the event
- Does this linq-to-sql query retrieve all records and then pick one, or retrieve just one?
- How can i access an object in a list in c# query with ef?
- Cutting down LINQ Query time
- TakeWhile, at least n elements
- comma separated list items with brackets in linq