Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i use Linq to iterate/filter my web.config AppSettings?

i'm trying to figure out how I can use Linq to filter out some of my appsettings from my web.config file.

i'm trying to do something like the following (which has wrong syntax) :-

var query = from q in System.Web.Configuration.WebConfigurationManager.AppSettings.Keys
            where q.StartsWith("Foo")
            select q);

what have I done wrong?

edit: added screenie (here's a link to it)

alt text http://img21.imageshack.us/img21/5516/errorji.png

like image 651
Pure.Krome Avatar asked Sep 12 '09 04:09

Pure.Krome


People also ask

When should you use LINQ in your program?

Readable code: LINQ makes the code more readable so other developers can easily understand and maintain it. Standardized way of querying multiple data sources: The same LINQ syntax can be used to query multiple data sources. Compile time safety of queries: It provides type checking of objects at compile time.

Can we use LINQ in .NET core?

NET Core LINQ stands for Language Integrated Query. Language Integrated Query is one structured query that is used to retrieve data from the database and other different sources and formats. LINQ tutorials will assist you to find out the LINQ language using topics that go from basic to advanced.

Can we use LINQ in asp net?

You can use LINQ through the LinqDataSource control, through the ObjectDataSource control, or by creating LINQ queries. When you use LINQ in a Web application, you might have to change the policy files for code-access security.

What is the use of appSettings in Web config?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.


2 Answers

Try this if you want the values:

var settings = System.Web.Configuration.WebConfigurationManager.AppSettings;

var query = from string q in settings.Keys
            where q.StartsWith("Foo")
            select settings[q];
like image 115
DSO Avatar answered Oct 24 '22 02:10

DSO


Could be because KeysCollection only implements IEnumerable not IEnumerable<T>. Try using the Cast method on the Keys property first, something like:

var query = from q in System.Web.Configuration.WebConfigurationManager.AppSettings.Keys.Cast<string>()
        where q.StartsWith("Foo")
        select q;
like image 1
Simon Fox Avatar answered Oct 24 '22 04:10

Simon Fox