December 2007
Intermediate to advanced
896 pages
19h 57m
English
Sets of data can be stored in many different locations, such as configuration files. You want to be able to query your configuration files for sets of information.
Use LINQ to query against the configuration sections. In the example below, this is done by retrieving all chapter titles with even numbers and the word "and" in the title from the custom configuration section containing chapter information:
CSharpRecipesConfigurationSection recipeConfig =
ConfigurationManager.GetSection("CSharpRecipesConfiguration") as
CSharpRecipesConfigurationSection;
var expr = from ChapterConfigurationElement chapter in
recipeConfig.Chapters.OfType<ChapterConfigurationElement>()
where (chapter.Title.Contains("and")) && ((int.Parse(chapter.Number) % 2)
== 0)
select new
{
ChapterNumber = "Chapter " + chapter.Number,
chapter.Title
};
foreach (var chapterInfo in expr)
{
Console.WriteLine(chapterInfo.ChapterNumber + ": " + chapterInfo.Title);
}The configuration section being queried looks like this:
<CSharpRecipesConfiguration CurrentEdition="3"> <Chapters> <add Number="1" Title="Language Integrated Query (LINQ)"/> <add Number="2" Title="Strings and Characters"/> <add Number="3" Title="Classes and Structures"/> <add Number="4" Title="Generics"/> <add Number="5" Title="Collections"/> <add Number="6" Title="Iterators, Partial Types and Partial Methods"/> <add Number="7" Title="Exception Handling"/> <add Number="8" Title="Diagnostics"/> <add Number="9" ...
Read now
Unlock full access