Gra*_*ton 61 .net linq namevaluecollection
如何使NameValueCollectionLINQ查询运算符可以访问,例如where,join,groupby?
我试过以下:
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();
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个ArgumentException告诉我源不是IEnumerable <>.
Bry*_*tts 90
你需要的"升降机"的非通用IEnumerable到IEnumerable<string>.有人建议您使用OfType但这是一种过滤方法.你正在做的是相当于一个演员,有Cast运营商:
var fields = RequestFields().Cast<string>();
Run Code Online (Sandbox Code Playgroud)
正如弗兰斯指出的那样,这只能提供对密钥的访问.您仍然需要为值集合索引.这是一个KeyValuePair从以下方法中提取s 的扩展方法NameValueCollection:
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]));
}
Run Code Online (Sandbox Code Playgroud)
编辑:为响应@Ruben Bartelink的请求,以下是如何使用以下方法访问每个密钥的完整值集ToLookup:
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);
}
Run Code Online (Sandbox Code Playgroud)
或者,使用C#7.0元组:
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);
}
Run Code Online (Sandbox Code Playgroud)
Amy*_*y B 11
AsQueryable必须采取IEnumerable<T>一个通用的.NameValueCollection工具IEnumerable,这是不同的.
而不是这个:
{
NameValueCollection nvc = RequestFields();
IQueryable queryable = nvc.AsQueryable();
}
Run Code Online (Sandbox Code Playgroud)
试试OfType(它接受非通用接口)
{
NameValueCollection nvc = RequestFields();
IEnumerable<string> canBeQueried = nvc.OfType<string>();
IEnumerable<string> query =
canBeQueried.Where(s => s.StartsWith("abc"));
}
Run Code Online (Sandbox Code Playgroud)
字典实际上可能更接近您想要使用的字典,因为它实际上将填充NameValueCollection填充的更多角色.这是Bryan Watts解决方案的变体:
public static class CollectionExtensions
{
public static IDictionary<string, string> ToDictionary(this NameValueCollection source)
{
return source.Cast<string>().Select(s => new { Key = s, Value = source[s] }).ToDictionary(p => p.Key, p => p.Value);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我迟到了,但只是想添加我的答案,不涉及.Cast扩展方法,而是使用AllKeys属性:
var fields = RequestFields().AllKeys;
Run Code Online (Sandbox Code Playgroud)
这将允许以下扩展方法:
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]));
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于未来的访客
| 归档时间: |
|
| 查看次数: |
32564 次 |
| 最近记录: |