首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使NameValueCollection可供链接查询访问

使NameValueCollection可供链接查询访问
EN

Stack Overflow用户
提问于 2008-12-24 08:28:47
回答 7查看 36.9K关注 0票数 65

如何使LINQ查询操作符,如where,join,groupby访问NameValueCollection

我尝试了以下几种方法:

代码语言:javascript
复制
private NameValueCollection RequestFields()
{
    NameValueCollection nvc = new NameValueCollection()
                                  {
                                      {"emailOption: blah Blah", "true"},
                                      {"emailOption: blah Blah2", "false"},
                                      {"nothing", "false"},
                                      {"nothinger", "true"}
                                  };
    return nvc;

}

public void GetSelectedEmail()
{
    NameValueCollection nvc = RequestFields();
    IQueryable queryable = nvc.AsQueryable();
}

但是ArgumentException告诉我的来源不是IEnumerable<>

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2008-12-28 18:38:04

您需要将非泛型IEnumerable“提升”为IEnumerable<string>。有人建议您使用OfType,但这是一种过滤方法。您所做的等同于强制转换,其中包含Cast运算符:

代码语言:javascript
复制
var fields = RequestFields().Cast<string>();

正如Frans所指出的,这只提供了对密钥的访问。您仍然需要为这些值索引到集合中。下面是一个从NameValueCollection中提取KeyValuePair的扩展方法

代码语言:javascript
复制
public static IEnumerable<KeyValuePair<string, string>> ToPairs(this NameValueCollection collection)
{
    if(collection == null)
    {
        throw new ArgumentNullException("collection");
    }

    return collection.Cast<string>().Select(key => new KeyValuePair<string, string>(key, collection[key]));
}

编辑:为了响应@Ruben Bartelink的请求,下面是如何使用ToLookup访问每个键的全套值

代码语言:javascript
复制
public static ILookup<string, string> ToLookup(this NameValueCollection collection)
{
    if(collection == null)
    {
        throw new ArgumentNullException("collection");
    }

    var pairs =
        from key in collection.Cast<String>()
        from value in collection.GetValues(key)
        select new { key, value };

    return pairs.ToLookup(pair => pair.key, pair => pair.value);
}

或者,使用C# 7.0元组:

代码语言:javascript
复制
public static IEnumerable<(String name, String value)> ToTuples(this NameValueCollection collection)
{
    if(collection == null)
    {
        throw new ArgumentNullException("collection");
    }

    return
        from key in collection.Cast<string>()
        from value in collection.GetValues(key)
        select (key, value);
}
票数 96
EN

Stack Overflow用户

发布于 2008-12-24 15:15:23

AsQueryable必须接受泛型类型的IEnumerable<T>NameValueCollection实现了IEnumerable,这是不同的。

而不是这样:

代码语言:javascript
复制
{
    NameValueCollection nvc = RequestFields();
    IQueryable queryable = nvc.AsQueryable();
}

试试OfType (它接受非泛型接口)

代码语言:javascript
复制
{
    NameValueCollection nvc = RequestFields();
    IEnumerable<string> canBeQueried = nvc.OfType<string>();
    IEnumerable<string> query =
       canBeQueried.Where(s => s.StartsWith("abc"));
}
票数 11
EN

Stack Overflow用户

发布于 2013-09-17 23:59:43

我知道我来晚了,但我只是想补充一下我的答案,它不涉及.Cast扩展方法,而是使用AllKeys属性:

代码语言:javascript
复制
var fields = RequestFields().AllKeys;

这将允许使用以下扩展方法:

代码语言:javascript
复制
public static IEnumerable<KeyValuePair<string, string>> ToPairs(this NameValueCollection collection)
{
    if(collection == null)
    {
        throw new ArgumentNullException("collection");
    }

    return collection.AllKeys.Select(key => new KeyValuePair<string, string>(key, collection[key]));
}

希望这对任何未来的访问者有所帮助

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/391023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档