ConfigurationElementCollection and Linq

本文关键字:Linq and ConfigurationElementCollection | 更新日期: 2023-09-27 18:16:56

我写了一些自定义配置集合,元素等。现在,我想做一个简单的Linq语句:

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

我得到错误:

找不到源类型的查询模式的实现"MyNamespace.ServerDetails"。'Where' not found.

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;
    }
}

我错过了什么吗?我是否需要添加一些东西,以便我可以使用Linq与自定义配置元素?

顺便说一下,我有using System.Linq;定义,因为我在同一类中使用它。

ConfigurationElementCollection and Linq

好,考虑到它都是弱类型的,您需要显式调用Cast<>OfType<>,或者为range变量提供显式类型。您还需要在ServerDetails上指定ServerCollection属性。例如:

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

在他的IEnumerable实现时,我能够枚举我的ConfigurationElementCollection。

它看起来像这样(使用原来的问题):

public sealed class ServerCollection : ConfigurationElementCollection,
    IEnumerable<ServerElement>
{
    ...
    public new IEnumerator<ServerElement> GetEnumerator()
    {
        foreach (var key in this.BaseGetAllKeys())
        {
            yield return (ServerElement)BaseGet(key);
        }
    }
}

当我没有得到错误时:

找不到源类型' mynamspace . serverdetails '的查询模式的实现。'Where' not found

…我也不能使用LINQ迭代我的ConfigurationElementCollection。这个解决方案修复了我的问题,这样我就可以使用LINQ来迭代我的集合。

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

一个非常晚的答案,我会使用这个扩展类将任何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;
        }
    }
}

下面的例子
ConfigurationManager
   .GetSection("serverDetails"))
   .ServerCollection
   .ToEnumerable<ServerElement>()
   .FirstOrDefault(x => x.Name == serverName);