todictionary

How to convert IEnumerable of KeyValuePair<x, y> to Dictionary?

回眸只為那壹抹淺笑 提交于 2019-12-21 06:48:34
问题 Is there streamlined way to convert list/enumberable of KeyValuePair<T, U> to Dictionary<T, U> ? Linq transformation, .ToDictionary() extension did not work. 回答1: .ToDictionary(kvp=>kvp.Key,kvp=>kvp.Value); Isn't that much more work. 回答2: You can create your own extension method that would perform as you expect. public static class KeyValuePairEnumerableExtensions { public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source) { return

Convert an IOrderedEnumerable<KeyValuePair<string, int>> into a Dictionary<string, int>

那年仲夏 提交于 2019-12-04 15:01:18
问题 I was following the answer to another question, and I got: // itemCounter is a Dictionary<string, int>, and I only want to keep // key/value pairs with the top maxAllowed values if (itemCounter.Count > maxAllowed) { IEnumerable<KeyValuePair<string, int>> sortedDict = from entry in itemCounter orderby entry.Value descending select entry; sortedDict = sortedDict.Take(maxAllowed); itemCounter = sortedDict.ToDictionary<string, int>(/* what do I do here? */); } Visual Studio's asking for a

How to convert IEnumerable of KeyValuePair<x, y> to Dictionary?

爷,独闯天下 提交于 2019-12-03 22:12:16
Is there streamlined way to convert list/enumberable of KeyValuePair<T, U> to Dictionary<T, U> ? Linq transformation, .ToDictionary() extension did not work. .ToDictionary(kvp=>kvp.Key,kvp=>kvp.Value); Isn't that much more work. You can create your own extension method that would perform as you expect. public static class KeyValuePairEnumerableExtensions { public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source) { return source.ToDictionary(item => item.Key, item => item.Value); } } This is the best I could produce: public static

Convert an IOrderedEnumerable<KeyValuePair<string, int>> into a Dictionary<string, int>

為{幸葍}努か 提交于 2019-12-03 09:21:45
I was following the answer to another question , and I got: // itemCounter is a Dictionary<string, int>, and I only want to keep // key/value pairs with the top maxAllowed values if (itemCounter.Count > maxAllowed) { IEnumerable<KeyValuePair<string, int>> sortedDict = from entry in itemCounter orderby entry.Value descending select entry; sortedDict = sortedDict.Take(maxAllowed); itemCounter = sortedDict.ToDictionary<string, int>(/* what do I do here? */); } Visual Studio's asking for a parameter Func<string, int> keySelector . I tried following a few semi-relevant examples I've found online

C# ToDictionary lambda select index and element?

落爺英雄遲暮 提交于 2019-11-30 14:37:33
问题 I have a string like string strn = "abcdefghjiklmnopqrstuvwxyz" and want a dictionary like: Dictionary<char,int>(){ {'a',0}, {'b',1}, {'c',2}, ... } I've been trying things like strn.ToDictionary((x,i) => x,(x,i)=>i); ...but I've been getting all sorts of errors about the delegate not taking two arguments, and unspecified arguments, and the like. What am I doing wrong? I would prefer hints over the answer so I have a mental trace of what I need to do for next time, but as per the nature of

Convert Linq Query Result to Dictionary

只谈情不闲聊 提交于 2019-11-26 06:55:51
问题 I want to add some rows to a database using Linq to SQL, but I want to make a \"custom check\" before adding the rows to know if I must add, replace or ignore the incomming rows. I\'d like to keep the trafic between the client and the DB server as low as possible and minimize the number of queries. To do this, I want to fetch as little information as required for the validation, and only once at the beginning of the process. I was thinking of doing something like this, but obviously, it doesn