【【C#MVC工具】C#MVC中使用Dictionary】教程文章相关的互联网学习教程文章

c# – 使用DataContractJsonSerializer序列化Dictionary <>对象【代码】

我有以下字典<>宾语:Dictionary<String, object> parameters = new Dictionary<string, object>(); parameters.Add("username", "mike"); parameters.Add("password", "secret"); parameters.Add("persist", false);当我序列化它时:using (MemoryStream stream = new MemoryStream()) {DataContractJsonSerializer serializer = new DataContractJsonSerializer(parameters.GetType());serializer.WriteObject(stream, parameter...

C#Automapper Mapping Dictionary属性【代码】

我正在努力弄清楚如何根据Dictionary对象的键来映射值. 这是我到目前为止:.ForMember(dest => dest.Submitter,opts => opts.MapFrom(src => src.opened_by))注意:src.open_by是一个字典对象.我想按键搜索以获取映射到dest.Submitter的值 附加信息: 这是目标对象:public class Incident {public int Active { get; set; }public string Submitter { get; set; } }这是源对象:Dictionary<string, string> opened_by = new Dicti...

c# – 如何从WCF REST方法返回自定义类型值的Dictionary作为常规JSON对象?【代码】

假设我有一个看起来像这样的自定义类型:[DataContract] public class CompositeType {[DataMember]public bool HasPaid{get;set;}[DataMember]public string Owner{get;set;} }和一个如下所示的WCF REST接口:[ServiceContract] public interface IService1 {[OperationContract]Dictionary<string, CompositeType> GetDict(); }那么如何让我的方法实现返回一个看起来像这样的JSON对象…{"fred":{"HasPaid":false,"Owner":"Fred M...

C#Dictionary – 字典中没有给定的键【代码】

我目前正在尝试将Tiled(Tiled地图编辑器)地图文件中的游戏对象加载到我在C#中制作的游戏引擎中.我正在使用TiledSharp(链接到github here).它使用字典来保存我正在尝试加载的每个单独的图块(或“游戏对象”)的属性.但由于某些原因,当我遍历属性时出现错误,如果我检查它是否为空,我也会收到错误 这是我正在使用的代码片段:for (int l = 0; l < tmxMap.Tilesets[k].Tiles.Count; l++)// This line throws an errorif (tmxMap.Tileset...

c# – LINQ to Dictionary – 使用XML【代码】

我已经问了一个类似的问题,但是,这个问题是不同的,我试过了. 我试图使用Linq读取XML数据,然后我解决了这个问题,但是,我需要选择多个值并将它们存储到Dictionary中.我试过了(评论是我的尝试),但我无法弄明白并得到如下错误:System.NullReferenceException: Object reference not set to an instance of an objectat dsdsdsds.MainClass+c__AnonStorey0.<>m__0 (System.Xml.Linq.XElement datanum) [0x00000] in /Users/p/Projects/...

c# – 我对Dictionary的使用是否需要锁定?【代码】

许多线程都可以访问摘要.每个线程都有一个唯一的密钥用于访问字典;Dictionary<string, List<Result>> summary;我是否需要锁定以进行以下操作? > summary [key] = new List< Result>()> summary [key] .Add(new Result()); 似乎我不需要锁定,因为每个线程将使用不同的密钥访问字典,但是由于将新记录同时添加到字典与其他步骤,因此(1)不会出现问题吗?解决方法:是的,你需要使用锁定. 字典对于添加操作不是线程安全的.如果您使用的是...

c# – 对可能未初始化的Dictionary元素执行加号等于操作的简明方法【代码】

我正在寻找一种扩展方法或任何其他建议,可以帮助我尽可能简洁地使用此代码.foreach( Layer lyr in this.ProgramLayers )foreach( UWBCEvent evt in this.BcEvents.IncludedEvents )EventGroupLayerLosses[new EventGroupIDLayerTuple(evt.EventGroupID, lyr)] += GetEL(evt.AsIfs, lyr.LimitInMillions, lyr.AttachmentInMillions);上面的代码有一个相当明确的目的,我用一个复合键将值分成组.但是,此代码将失败,因为字典最初为空,并...

c# – 如何在委托中使用Dictionary【代码】

我有一个字典,我想根据不同的条件进行过滤,例如IDictionary<string, string> result = collection.Where(r => r.Value == null).ToDictionary(r => r.Key, r => r.Value);我想将Where子句作为参数传递给执行实际过滤的方法,例如private static IDictionary<T1, T2> Filter<T1, T2>(Func<IDictionary<T1, T2>, IDictionary<T1, T2>> exp, IDictionary<T1, T2> col) {return col.Where(exp).ToDictionary<T1, T2>(r => r.Key, r => r...

c# – LINQ to Entities无法识别方法’System.Collections.Generic.Dictionary`2 [System.Int32,System.String] To【代码】

我试图从DB中检索EmployeeDTO列表,这些列表存储在Employee表中.每个员工都可以拥有一个或多个专业.专业存储在OrganizationSpecialtyType中. Employee和OrganizationSpecialtyType通过EmployeeSpecialty表与“many to many”相关联. 我使用以下查询并获得标题中的异常:var q = _context.Employee.Where(p => employeeEMIIDs.Contains(p.EmployeeID)).Select(p => new EmployeeDTO{EmployeeID = p.EmployeeID,GenderTypeID = p.Gend...

c#Dictionary,类为Key【代码】

我正在学习电子工程,我是c#的初学者.我已经测量了数据,并希望以2个方式存储它.我以为我可以制作这样的字典:Dictionary<Key, string>dic = new Dictionary<Key, string>(); “Key”在这里是一个拥有两个int变量的自有类.现在我想将数据存储在这个词典中,但到目前为止它还没有用.如果我想使用特殊键读取数据,则错误报告会说,密钥在字典中不可用. 这里的班级钥匙:public partial class Key {public Key(int Bahn, int Zeile) {myBah...

C# Dictionary使用【代码】【图】

Dictionary<string, int> illegParking = new Dictionary<string, int>(); 键:inData.LOTID 值:inData.ISILLEGPARKING 1、判断键存不存在。 dictionary中是不允许有重复项的,这样才能按key索引到唯一一个value。 if (illegParking.ContainsKey(inData.LOTID)){illegParking[inData.LOTID] = inData.ISILLEGPARKING;}else{illegParking.Add(inData.LOTID, inData.ISILLEGPARKING);}View Code 2、几种遍历方式: Dictionary<strin...

c# – 根据for循环位置创建Dictionary的键【代码】

我要去一个目录拿起一些文件,然后将它们添加到一个字典中. 第一次在循环中,键需要是A,第二次是B等.在26 / Z处,数字表示不同的字符,从33开始,它以小写字母a开始,最多为49,即小写q. 没有大量的if声明,如果我= = 1然后Key是’A’等等我怎么能保持这个代码整洁?解决方法:听起来你只需要保留你所到之处的索引,然后是一些映射函数:int index = 0; foreach (...) {...string key = MapIndexToKey(index);dictionary[key] = value;index+...

c# – 默认构造函数创建的`Dictionary`是否使用哈希码?

我需要使用我编写的类作为Dictionary的键的类型 我读了documentation on MSDN about the default constructor of DictionaryDictionary<TKey, TValue> requires an equality implementation todetermine whether keys are equal. This constructor uses the defaultgeneric equality comparer, EqualityComparer<T>.Default. If type TKey implements the System.IEquatable<T> generic interface, thedefault equality comparer u...

c# – 将Dictionary转换为HashEntry【代码】

我试图使用hashSet方法,它需要HashEntry []数组.HashSet(RedisKey key, HashEntry[] hashFields, CommandFlags flags = CommandFlags.None);我试图这样做,但这显然不起作用…… 我有字典值HashEntry[] hash = new HashEntry[value.Count]();int index = 0;foreach (var item in value){hash[index].Name = item.Key;hash[index].Value = item.Value;index++;}解决方法:HashEntry是不可变的;你需要:hash[index++] = new HashEntry(...

c# – 为什么Dictionary没有实现Add(KeyValuePair)【代码】

IDictionary< TKey,TValue>从接口ICollection< KeyValuePair< TKey,TValue>>延伸,因此它具有Add(KeyValuePair< TKey,TValue>项)方法:IDictionary<string, object> dic = new Dictionary<string, object>(); dic.Add(new KeyValuePair<string, object>("number", 42)); // Compiles fine然而,尽管Dictionary< TKey,Tvalue>实现IDictionary< TKey,TValue>,它没有这个方法重载:Dictionary<string, object> dic = new Dictionary<str...