简体   繁体   English

LINQ的ConfigurationElementCollection

[英]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]

the problem 问题

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默认实现,因此它仅打印类型名称-这就是为什么您会得到奇怪的答案的原因。

basic solution: 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视为一种惰性数组/列表-它只是在等待您提取其项。

expecting exactly one element? 只是一个元素?

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

some fun with overloads/linq 重载/ linq带来一些乐趣

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: 而且,如果您更深入地研究重载,您会发现可以将WhereFirst (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.

相关问题 ConfigurationElementCollection 和 Linq - ConfigurationElementCollection and Linq 从ConfigurationPropertyAttribute中获取ConfigurationElementCollection中的GetKey? - GetKey in ConfigurationElementCollection from ConfigurationPropertyAttribute? 具有基本类型的ConfigurationElementCollection - ConfigurationElementCollection with primitive types 如何使用ConfigurationElementCollection实现ConfigurationSection - How to implement a ConfigurationSection with a ConfigurationElementCollection 隐式ConfigurationElementCollection部分 - Implicit ConfigurationElementCollection sections 自定义 ConfigurationElementCollection 抛出 TargetInvocationException - Custom ConfigurationElementCollection Throw TargetInvocationException ConfigurationElementCollection:通过lambda表达式过滤 - ConfigurationElementCollection: filtering through lambda expression 如何在ConfigurationElementCollection中拥有自定义属性? - how to have custom attribute in ConfigurationElementCollection? ConfigurationElementCollection对象是否可以为其元素包含另一个ConfigurationElementCollection对象? - Can ConfigurationElementCollection object contain for its elements another ConfigurationElementCollection objects? ConfigurationElementCollection中的无键元素 - Key-less elements in ConfigurationElementCollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM