64301

Use Linq to iterate over ConfigurationManager.ConnectionStrings?

Is it possible to do something like this?

var strings = ConfigurationManager.ConnectionStrings; var names = (from d in strings select new ConnectionName(d.Name));

Answer1:

Yes, but because ConnectionStrings does not implement a strongly typed IEnumerable, you have to tell LINQ what type the collection contains.

Use either from ConnectionStringSettings d in strings or ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>().

Answer2:

You have to Cast it to its type as it is IEnumerable not IEnumerable<T> See Enumerable.Cast :

Casts the elements of an IEnumerable to the specified type.

var t = from c in connectionString.Cast<System.Configuration.ConnectionStringSettings>() select c.Name;

Recommend

  • Cannot attach to a network adapter on one VM, same code works on another VM
  • SQL Filestream Access Denied
  • Best way to pass variable from config file in .net MVC4 to Angular scope
  • Create table before the dataflow in BIML
  • How To retrieve a control in a Master Page from Content Page
  • Synchronous XHR deprecation
  • Why does opening a connection throw a distributed transactions error in MySQL? (.NET Connector)
  • Play HTML audio in Internet Explorer?
  • Need advice in designing tables in SQL-Server
  • Write a function that calculate the sum of integers in a list in Erlang
  • JFreeChart BarChart - Category Markers
  • Wcf NHibernate Session management
  • Regex: Match everything except backreference
  • ViewData, ViewBag and TempData violates MVC? [closed]
  • PostgreSQL 9.1 timezones
  • ResponseBuilder is not working when used with entity object
  • Finding All Combinations (cartesian product) of list values in vb.net
  • Getting EOFError along with exceptions when using ftplib
  • PDF using WCF Restful Services
  • Escaping single quotes in JDBC with MySql
  • How to open multiple instances of a program in Linux
  • Many to Many in Linq using Dapper
  • Multiple Left Join LINQ-to-entities
  • Efficient algorithm to find additions and removals from 2 collections
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • Trying to string.Join an IList
  • Exception creating JSON with LINQ
  • KnockoutObservableArray with typed elements in TypeScript
  • How to make R's read_csv2() recognise the text characters properly
  • Cannot get the UserManager class
  • Linq Merge lists
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Insert new calendar with SyncAdapter- Calendar API Android
  • C: Incompatible pointer type initializing
  • Django simple Captcha “No module named fields” error
  • Set the selected item in dropdownlist in MVC3
  • C# - Serializing and deserializing static member
  • Bug in WPF DataGrid
  • Incrementing object id automatically JS constructor (static method and variable)
  • Compare two NSDates in iPhone