简体   繁体   English

ConfigurationElementCollection 和 Linq

[英]ConfigurationElementCollection and Linq

I've written some custom configuration collections, elements etc. Now, I'd like to do a simple Linq statement:我已经编写了一些自定义配置集合、元素等。现在,我想做一个简单的 Linq 语句:

ServerDetails servers = ConfigurationManager.GetSection("serverDetails") as ServerDetails;
var server = from s in servers
             where s.Name == serverName
             select s;

I get the error:我收到错误:

Could not find an implementation of the query pattern for source type 'MyNamespace.ServerDetails'.找不到源类型“MyNamespace.ServerDetails”的查询模式的实现。 'Where' not found.找不到“哪里”。

The ServerElement has two properties: ServerElement有两个属性:

public class ServerElement : ConfigurationElement
{
    [ConfigurationProperty("ip")]
    public string IP
    {
        get { return (string)base["ip"]; }
        set { base["ip"] = value; }
    }

    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }
}

ServerDetails

public sealed class ServerDetails : ConfigurationSection
{
    [ConfigurationProperty("ServerCollection")]
    [ConfigurationCollection(typeof(ServerCollection), AddItemName = "add")]
    public ServerCollection ServerCollection
    {
        get { return this["ServerCollection"] as ServerCollection; }
    }
}

ServerCollection

public sealed class ServerCollection : ConfigurationElementCollection
{
    public void Add(ServerElement ServerElement)
    {
        this.BaseAdd(ServerElement);
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ServerElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ServerElement)element).Name;
    }
}

Am I missing something?我错过了什么吗? Do I need to add something in so that I can use Linq with a custom configuration element?我是否需要添加一些东西,以便我可以将 Linq 与自定义配置元素一起使用?

By the way, I have using System.Linq;顺便说一句,我using System.Linq; defined as I'm using it else where within the same class.定义为我在同一个类中的其他地方使用它。

Okay, given that it's all weakly typed, you'll need to either call Cast<> or OfType<> explicitly, or give an explicit type to the range variable.好的,鉴于它都是弱类型的,您需要显式调用Cast<>OfType<> ,或者为范围变量提供显式类型。 You'll also need to specify the ServerCollection property on your ServerDetails .您还需要在ServerDetails上指定ServerCollection属性。 For example:例如:

ServerDetails servers = (ServerDetails) ConfigurationManager.GetSection("serverDetails");
var server = from ServerElement s in servers.ServerCollection
             where s.Name == serverName
             select s;

Using Brian Gideon's simple example of yield return in his IEnumerable<T> implementation, I was able to enumerate over my ConfigurationElementCollection.使用Brian Gideon在他的 IEnumerable<T> 实现中的简单收益返回示例,我能够枚举我的 ConfigurationElementCollection。

It would look something like this (using the original question):它看起来像这样(使用原始问题):

public sealed class ServerCollection : ConfigurationElementCollection,
    IEnumerable<ServerElement>
{
    ...

    public new IEnumerator<ServerElement> GetEnumerator()
    {
        foreach (var key in this.BaseGetAllKeys())
        {
            yield return (ServerElement)BaseGet(key);
        }
    }
}

While I was NOT getting the error:虽然我没有收到错误:

Could not find an implementation of the query pattern for source type 'MyNamespace.ServerDetails'.找不到源类型“MyNamespace.ServerDetails”的查询模式的实现。 'Where' not found找不到“哪里”

...I was not able to iterate over my ConfigurationElementCollection using LINQ, either. ...我也无法使用 LINQ 迭代我的 ConfigurationElementCollection。 This solution fixed my problem so that I could use LINQ to iteration over my collection.此解决方案解决了我的问题,以便我可以使用 LINQ 对我的集合进行迭代。

 var server = ((ServerDetails) ConfigurationManager.GetSection("serverDetails")).
      ServerCollection.Cast<ServerElement>().FirstOrDefault(x => x.Name == serverName);

A very late answer, I would use this extension class to turn any ConfigurationElementCollection into an IEnumerable safely.一个很晚的答案,我会使用这个扩展类将任何 ConfigurationElementCollection 安全地转换为 IEnumerable。

public static class ConfigurationElementCollectionExtension
{
    public static IEnumerable<T> ToEnumerable<T>(this ConfigurationElementCollection collection)
    {
        foreach (var element in collection)
        {
            if (element is T)
                yield return (T)element;

            yield return default;
        }
    }
}

Example usage below下面的示例用法

ConfigurationManager
   .GetSection("serverDetails"))
   .ServerCollection
   .ToEnumerable<ServerElement>()
   .FirstOrDefault(x => x.Name == serverName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ的ConfigurationElementCollection - ConfigurationElementCollection 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