标签: idictionary

将IDictionary <string,string>键转换为小写(C#)

我有一个获取IDictionary作为参数的方法.现在我想提供一个从这个字典中检索值的方法,但它应该是case-invariant.

所以我现在的解决方案就是有一个静态函数循环遍历键并将它们转换为下面这样的:

private static IDictionary<ILanguage, IDictionary<string, string>> ConvertKeysToLowerCase(
    IDictionary<ILanguage, IDictionary<string, string>> dictionaries)
{
    IDictionary<ILanguage, IDictionary<string, string>> resultingConvertedDictionaries 
        = new Dictionary<ILanguage, IDictionary<string, string>>();
    foreach(ILanguage keyLanguage in dictionaries.Keys)
    {
        IDictionary<string, string> convertedDictionatry = new Dictionary<string, string>();
        foreach(string key in dictionaries[keyLanguage].Keys)
        {
            convertedDictionatry.Add(key.ToLower(), dictionaries[keyLanguage][key]);
        }
        resultingConvertedDictionaries.Add(keyLanguage, convertedDictionatry);
    }
    return resultingConvertedDictionaries;
}
Run Code Online (Sandbox Code Playgroud)

现在,这没关系,但仍然是一大堆代码与我的"干净和高效"的想法相矛盾.你知道任何替代方法,以便字典的.ContainsKey()方法不区分套管吗?

.net c# idictionary

5
推荐指数
1
解决办法
1万
查看次数

如何使用LINQ(C#3.0)更改IDictionary的内容

如何使用C#3.0(Linq,Linq扩展)更改IDictionary的内容?

var enumerable = new int [] { 1, 2};
var dictionary = enumerable.ToDictionary(a=>a,a=>0);
//some code
//now I want to change all values to 1 without recreating the dictionary
//how it is done?
Run Code Online (Sandbox Code Playgroud)

.net c# linq idictionary c#-3.0

5
推荐指数
1
解决办法
1428
查看次数

字典<StudentType,List <Student >>到IDictionary <StudentType,IList <Student >>?

请考虑以下代码:

class Student
{
}

enum StudentType
{
}

static void foo(IDictionary<StudentType, IList<Student>> students)
{   
}

static void Main(string[] args)
{
    Dictionary<StudentType, List<Student>> studentDict = 
                     new Dictionary<StudentType, List<Student>>();

    foo(studentDict);

    ...
}
Run Code Online (Sandbox Code Playgroud)

有错误:

错误CS1503:参数'1':无法从'System.Collections.Generic.Dictionary>'转换为'System.Collections.Generic.IDictionary>'

有没有办法调用foo函数?

c# dictionary ilist list idictionary

5
推荐指数
1
解决办法
1998
查看次数

ASP.NET MVC中htmlAttributes的匿名类和IDictionary <string,object>之间的区别?

例如,如果检查这两个扩展方法,唯一的区别是htmlAttributes的类型,因此您可以通过两种不同的方式传递htmlAttributes:

public static MvcHtmlString TextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    IDictionary<string, object> htmlAttributes);

public static MvcHtmlString TextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    object htmlAttributes);
Run Code Online (Sandbox Code Playgroud)

并以下列方式之一使用它们:

@Html.TextBoxFor(model => model.TagLine,
    new { @placeholder = "We live to make art." })

@Html.TextBoxFor(model => model.TagLine,
    new Dictionary<string, object> { 
        { "placeholder", "We live to make art." } })
Run Code Online (Sandbox Code Playgroud)

我检查过MVC源代码,我知道在后台他们使用相同的方法,但是接受匿名对象的方法HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes)用于使匿名对象成为字典.

在我看来,使用匿名对象的视图更清晰.你们觉得怎么样?使用匿名对象有什么缺点吗?

c# asp.net-mvc idictionary asp.net-mvc-3

5
推荐指数
1
解决办法
3229
查看次数

System.Collections.Generic.KeyNotFoundException:给定的键不在字典中

我在对方法执行单元测试时收到上述错误消息.我知道问题出在哪里,我只是不知道为什么它不在字典中.

这是字典:

var nmDict = xelem.Descendants(plantNS + "Month").ToDictionary(
    k => new Tuple<int, int, string>(int.Parse(k.Ancestors(plantNS + "Year").First().Attribute("Year").Value), Int32.Parse(k.Attribute("Month1").Value), k.Ancestors(plantNS + "Report").First().Attribute("Location").Value.ToString()),
    v => { 
             var detail = v.Descendants(plantNS + "Details").First();
             return new HoursContainer
             {
                 BaseHours = detail.Attribute("BaseHours").Value,
                 OvertimeHours = detail.Attribute("OvertimeHours").Value,
                 TotalHours = float.Parse(detail.Attribute("BaseHours").Value) + float.Parse(detail.Attribute("OvertimeHours").Value)
             };
         });

var mergedDict = new Dictionary<Tuple<int, int, string>, HoursContainer>();

foreach (var item in nmDict)
{
    mergedDict.Add(Tuple.Create(item.Key.Item1, item.Key.Item2, "NM"), item.Value);
}


var thDict = xelem.Descendants(plantNS + "Month").ToDictionary(
    k => new Tuple<int, int, string>(int.Parse(k.Ancestors(plantNS + "Year").First().Attribute("Year").Value), Int32.Parse(k.Attribute("Month1").Value), …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-xml idictionary linq-to-sql

5
推荐指数
1
解决办法
5万
查看次数

使用隐式DictionaryEntry迭代IDictionary

考虑以下代码:

var variables = System.Environment.GetEnvironmentVariables();
foreach (DictionaryEntry vari in variables)
{
    Console.WriteLine(vari.Key);
    Console.WriteLine(vari.Value);
}
Run Code Online (Sandbox Code Playgroud)

它工作正常.既然variablesIDictionary,它由和DictionaryEntry,object Key和组成object Value.

为什么我不打字foreach(var vari in variables)?它给了我

error CS1061: 'object' does not contain a definition for 'Key/Value'...
Run Code Online (Sandbox Code Playgroud)

这看起来很奇怪,我无法找到这种行为的原因.DictionaryEntry是一个struct,但我可以迭代List<DictionaryEntry>一切.当然我理解这IDictionary不是通用的,但手册说它包含DictionaryEntries,所以应该可以使用var......

c# idictionary

5
推荐指数
1
解决办法
2237
查看次数

IDictionary接口的目的

IDictionary接口有什么需要.如何初始化IDictionary接口.毕竟它只是一个界面.以下代码段来自msdn.我无法理解.

IDictionary<string, string> openWith = new Dictionary<string, string>();
Run Code Online (Sandbox Code Playgroud)

c# idictionary

4
推荐指数
3
解决办法
2677
查看次数

使用String键绑定到索引属性

假设我想绑定到TKey与XAML字符串的字典:

<Label DataContext="{MyDictionary}" Content="{Binding Item("OK")}" />
Run Code Online (Sandbox Code Playgroud)

不行.

我该怎么办?

我说的是物品("钥匙")

vb.net wpf xaml dictionary idictionary

4
推荐指数
1
解决办法
3620
查看次数

删除与IDictionary中的条件匹配的所有项目

我正在尝试删除符合条件的IDictionary对象中的所有元素.

例如,IDictionary包含一组键和相应的值(比如说80个对象).键是字符串,值可以是不同类型(想想使用directshow从wtv文件中提取元数据).

一些键包含文本"thumb",例如thumbsize,startthumbdate等.我想删除IDictionary中包含单词thumb的所有对象.

我在这里看到的唯一方法是使用.Remove方法手动指定每个键名.

有没有办法让所有对象包含单词thumb的对象,并将它们从IDictionary对象中删除.

代码如下所示:

IDictionary sourceAttrs = editor.GetAttributes();
Run Code Online (Sandbox Code Playgroud)

GetAttributes定义为:

public abstract IDictionary GetAttributes();
Run Code Online (Sandbox Code Playgroud)

我无法控制GetAttributes,它返回一个IDictionary对象,我只是通过在调试时查看它来了解内容.(可能是HashTable)

更新:感谢Tim的最终答案:

sourceAttrs = sourceAttrs.Keys.Cast<string>()
                 .Where(key => key.IndexOf("thumb", StringComparison.CurrentCultureIgnoreCase) == -1)
                 .ToDictionary(key => key, key => sourceAttrs[key]);
Run Code Online (Sandbox Code Playgroud)

c# idictionary match

4
推荐指数
1
解决办法
761
查看次数

IEnumerable <T>到IDictionary <U,IEnumerable <T >>

什么是最有效的方式转换IEnumerable<T>IDictionary<U, IEnumerable<T>>

其中U是例如Guid,其信息保存在T的属性中.

基本上,这会创建一个列表字典,其中原始列表中的所有项目都是根据对象内属性中的值进行分组的.

对象定义:

class myObject
{
    public Guid UID { get; set; }

    // other properties
}
Run Code Online (Sandbox Code Playgroud)

从...开始:

IEnumerable<myObject> listOfObj;
Run Code Online (Sandbox Code Playgroud)

结束于:

IDictionary<Guid, IEnumerable<myObject>> dictOfLists;
Run Code Online (Sandbox Code Playgroud)

Whereby listOfObj包含具有许多不同但有时重叠的UID属性值的对象.

.net c# ienumerable type-conversion idictionary

3
推荐指数
1
解决办法
1012
查看次数