[英]ConfigurationElementCollection LINQ
I have some problem with LinQ. 我对LinQ有问题。 How can I get id by name in query or some LinQ condition? 如何在查询或某些LinQ条件下按名称获取ID? This is try to get: 这是尝试获得:
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());
}
This is config. 这是配置。
<?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>
But this is output: 但这是输出:
System.Linq.Enumerable+WhereSelectEnumerableIterator`2[ConsoleApplication2.JobElement,System.Int32] System.Linq.Enumerable + WhereSelectEnumerableIterator`2 [ConsoleApplication2.JobElement,System.Int32]
of course it does - you are printing the Enumerable
当然可以-您正在打印Enumerable
As there is no default implementation for ToString
on the generic interface it just prints the type-name - that's why you get the strange answer. 由于在通用接口上没有针对ToString
默认实现,因此它仅打印类型名称-这就是为什么您会得到奇怪的答案的原因。
foreach
基本解决方案: foreach
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;
foreach(var item in query)
Console.WriteLine(item.ToString());
}
If it helps think query
as a kind of lazy array/list - it's just waiting for you to pull it's items out. 如果它有助于将query
视为一种惰性数组/列表-它只是在等待您提取其项。
By the way - maybe you are expecting to get exactly one result. 顺便说一句-也许您期望得到一个结果。 In this case you can use .Single
like this: 在这种情况下,您可以像这样使用.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());
}
This will throw if there is not exactly one element in the result. 如果结果中不完全是一个元素,则将抛出该异常。 Alternatives are: 替代方法是:
.First
(will give you the first element and throws if here are none) .First
(将为您提供第一个元素,如果不包含则抛出) .FirstOrDefault
(will give you the first element or the default value - most likely null
if there are no items) .FirstOrDefault
(将为您提供第一个元素或默认值-如果没有项目,则很可能为null
) And if you poke in even deeper into the overloads you will see that you can combine the Where
and First
(etc.) parts like this: 而且,如果您更深入地研究重载,您会发现可以将Where
和First
(etc.)部分组合如下:
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);
}
Have fun 玩得开心
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.