C# 配置元素集合LINQ

C# 配置元素集合LINQ,c#,linq,app-config,C#,Linq,App Config,我和林克有些问题。如何在查询或某些LinQ条件中按名称获取id?这是一种尝试: var section = ConfigurationManager.GetSection("jobSection"); if (section != null) { var jobs = (section as JobSection).Jobs; var item = from JobElement je in jobs where je.Name == "Job Name A" select j

我和林克有些问题。如何在查询或某些LinQ条件中按名称获取id?这是一种尝试:

var section = ConfigurationManager.GetSection("jobSection");

if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var item = from JobElement je in jobs where je.Name == "Job Name A" select je.Id;
    Console.WriteLine(item.ToString());
}
这是配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="jobSection" type="ConsoleApplication2.JobSection, ConsoleApplication2" />
    </configSections>
    <jobSection>
        <jobs>
            <job id="1" name="Job Name A" />
            <job id="2" name="Job Name B" />
        </jobs>
    </jobSection>
</configuration>

但这是输出:

System.Linq.Enumerable+其中SelectEnumerableInterator`2[控制台应用程序2.JobElement,System.Int32]

问题 当然有-您正在打印可枚举的

由于在泛型接口上没有
ToString
的默认实现,所以它只打印类型名-这就是为什么会得到奇怪的答案

基本解决方案:
foreach
如果它有助于将
query
看作是一种懒惰的数组/列表,那么它只是在等待您将其项取出

只需要一个元素? 顺便说一句,也许你只希望得到一个结果。在这种情况下,您可以像这样使用
.Single

var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var query = 
       from JobElement je in jobs 
       where je.Name == "Job Name A" 
       select je.Id;
    var item = query.Single();
    Console.WriteLine(item.ToString());
}
var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var item = jobs.Cast<JobElement>()
                   .First(je => je.Name == "Job Name A");
    Console.WriteLine(item.Id);
}
如果结果中不只有一个元素,则会抛出此错误。备选方案包括:

  • .First
    (如果这里没有元素,则会给出第一个元素并抛出)
  • .FirstOrDefault
    (将为您提供第一个元素或默认值-如果没有项,则最有可能为
    null
重载/linq带来一些乐趣 如果你深入研究重载,你会发现你可以将
Where
First
(等等)这样的部分结合起来:

var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var query = 
       from JobElement je in jobs 
       where je.Name == "Job Name A" 
       select je.Id;
    var item = query.Single();
    Console.WriteLine(item.ToString());
}
var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var item = jobs.Cast<JobElement>()
                   .First(je => je.Name == "Job Name A");
    Console.WriteLine(item.Id);
}
var section=ConfigurationManager.GetSection(“作业节”);
if(节!=null)
{
var jobs=(作为JobSection的节)。jobs;
var item=jobs.Cast()
.First(je=>je.Name==“工作名称A”);
控制台写入线(项目Id);
}
玩得开心

解决问题 当然有-您正在打印可枚举的

由于在泛型接口上没有
ToString
的默认实现,所以它只打印类型名-这就是为什么会得到奇怪的答案

基本解决方案:
foreach
如果它有助于将
query
看作是一种懒惰的数组/列表,那么它只是在等待您将其项取出

只需要一个元素? 顺便说一句,也许你只希望得到一个结果。在这种情况下,您可以像这样使用
.Single

var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var query = 
       from JobElement je in jobs 
       where je.Name == "Job Name A" 
       select je.Id;
    var item = query.Single();
    Console.WriteLine(item.ToString());
}
var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var item = jobs.Cast<JobElement>()
                   .First(je => je.Name == "Job Name A");
    Console.WriteLine(item.Id);
}
如果结果中不只有一个元素,则会抛出此错误。备选方案包括:

  • .First
    (如果这里没有元素,则会给出第一个元素并抛出)
  • .FirstOrDefault
    (将为您提供第一个元素或默认值-如果没有项,则最有可能为
    null
重载/linq带来一些乐趣 如果你深入研究重载,你会发现你可以将
Where
First
(等等)这样的部分结合起来:

var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var query = 
       from JobElement je in jobs 
       where je.Name == "Job Name A" 
       select je.Id;
    var item = query.Single();
    Console.WriteLine(item.ToString());
}
var section = ConfigurationManager.GetSection("jobSection");
if (section != null)
{
    var jobs = (section as JobSection).Jobs;
    var item = jobs.Cast<JobElement>()
                   .First(je => je.Name == "Job Name A");
    Console.WriteLine(item.Id);
}
var section=ConfigurationManager.GetSection(“作业节”);
if(节!=null)
{
var jobs=(作为JobSection的节)。jobs;
var item=jobs.Cast()
.First(je=>je.Name==“工作名称A”);
控制台写入线(项目Id);
}
玩得开心