idictionary

Is there a better data structure than Dictionary if the values are objects and a property of those objects are the keys?

白昼怎懂夜的黑 提交于 2019-12-01 17:38:45
I have a Dictionary<int, object> where the int is a property of obj . Is there a better data structure for this? I feel like using a property as the key is redundant. This Dictionary<int, obj> is a field in a container class that allows for random indexing into the obj values based on an int id number. The simplified (no exception handling) indexer in the container class would look like: obj this[int id] { get{ return this.myDictionary[id];} } where myDictionary is the aforementioned Dictionary<int, obj> holding the objects. This may be the typical way of quick random access but I wanted to

Dynamically create anonymous object from list values c#

我是研究僧i 提交于 2019-12-01 11:44:14
I have a list (or can be array) of strings that I want to dynamically create an anonymous object from. How do I do this? var dataSet = new DataSet(); dataSet.ReadXml(@""); var dataTable = dataSet.Tables[0]; var dataRow = dataTable.Rows[0]; var keys = new List<string> {"Column1","Column2"}; var result = new {keys[0] = dataRow[keys[0]], keys[1] = dataRow[keys[1]]} So that list named "keys" is going to be created outside this method and can contain 1 to many values. I tried creating a dictionary and looping through the list and adding key/value pairs to the dictionary but then I couldnt figure

Dynamically create anonymous object from list values c#

冷暖自知 提交于 2019-12-01 08:39:20
问题 I have a list (or can be array) of strings that I want to dynamically create an anonymous object from. How do I do this? var dataSet = new DataSet(); dataSet.ReadXml(@""); var dataTable = dataSet.Tables[0]; var dataRow = dataTable.Rows[0]; var keys = new List<string> {"Column1","Column2"}; var result = new {keys[0] = dataRow[keys[0]], keys[1] = dataRow[keys[1]]} So that list named "keys" is going to be created outside this method and can contain 1 to many values. I tried creating a dictionary

Error: Can't convert from Dictionary to IDictionary

血红的双手。 提交于 2019-12-01 07:42:26
问题 Why is there an error Error 52 Argument 1: cannot convert from 'System.Collections.Generic.Dictionary>' to 'System.Collections.Generic.IDictionary>' Dictionary<string, List<string>> tempResultIDList = new Dictionary<string,List<string>>(); test(tempResultIDList); public bool test(IDictionary<string,IList<string>> a) { return true; } 回答1: Dictionary<string, List<string>> implements IDictionary<string, List<string>> , while you're trying to cast it to IDictionary<string, *I*List<string>> . It

Difference between anonymous class and IDictionary<string,object> for htmlAttributes in ASP.NET MVC?

不羁岁月 提交于 2019-11-30 21:41:53
For example if you check these two extension methods the only difference is type of htmlAttributes so you can pass your htmlAttributes in two different ways: 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); And use them in either of these ways: @Html.TextBoxFor(model => model.TagLine, new {

Difference between anonymous class and IDictionary<string,object> for htmlAttributes in ASP.NET MVC?

限于喜欢 提交于 2019-11-30 05:37:44
问题 For example if you check these two extension methods the only difference is type of htmlAttributes so you can pass your htmlAttributes in two different ways: 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

Create a Dictionary in xaml?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 14:20:00
Pseudo example: <Window> <Window.Tag> <x:Dictionary KeyType="{x:Type sys:String}" ValueType="{x:Type sys:Int32}"> <sys:DictionaryEntry Entry="{sys:DictionaryEntry Key0, 000}"/> <sys:DictionaryEntry Key="key1" Value="111"/> <sys:DictionaryEntry> <sys:DictionaryEntry.Key> <sys:String>Key2<sys:String> </sys:DictionaryEntry.Key> <sys:DictionaryEntry.Value> <sys:Int32>222</sys:Int32> </sys:DictionaryEntry.Value> </sys:DictionaryEntry> </x:Dictionary /> </Window.Tag> </Window> Thomas Levesque You can't use the Dictionary<TKey, TValue> class directly in XAML, because there's no way to specify the

Recreating a Dictionary from an IEnumerable<KeyValuePair<>>

我与影子孤独终老i 提交于 2019-11-27 03:21:50
I have a method that returns an IEnumerable<KeyValuePair<string, ArrayList>> , but some of the callers require the result of the method to be a dictionary. How can I convert the IEnumerable<KeyValuePair<string, ArrayList>> into a Dictionary<string, ArrayList> so that I can use TryGetValue ? method: public IEnumerable<KeyValuePair<string, ArrayList>> GetComponents() { // ... yield return new KeyValuePair<string, ArrayList>(t.Name, controlInformation); } caller: Dictionary<string, ArrayList> actual = target.GetComponents(); actual.ContainsKey("something"); Jon Skeet If you're using .NET 3.5 or

Create a Dictionary in xaml?

人走茶凉 提交于 2019-11-26 18:21:38
问题 Pseudo example: <Window> <Window.Tag> <x:Dictionary KeyType="{x:Type sys:String}" ValueType="{x:Type sys:Int32}"> <sys:DictionaryEntry Entry="{sys:DictionaryEntry Key0, 000}"/> <sys:DictionaryEntry Key="key1" Value="111"/> <sys:DictionaryEntry> <sys:DictionaryEntry.Key> <sys:String>Key2<sys:String> </sys:DictionaryEntry.Key> <sys:DictionaryEntry.Value> <sys:Int32>222</sys:Int32> </sys:DictionaryEntry.Value> </sys:DictionaryEntry> </x:Dictionary /> </Window.Tag> </Window> 回答1: You can't use

Mapping object to dictionary and vice versa

微笑、不失礼 提交于 2019-11-26 12:43:40
Are there any elegant quick way to map object to a dictionary and vice versa? Example: IDictionary<string,object> a = new Dictionary<string,object>(); a["Id"]=1; a["Name"]="Ahmad"; // ..... becomes SomeClass b = new SomeClass(); b.Id=1; b.Name="Ahmad"; // .......... Using some reflection and generics in two extension methods you can achieve that. Right, others did mostly the same solution, but this uses less reflection which is more performance-wise and way more readable: public static class ObjectExtensions { public static T ToObject<T>(this IDictionary<string, object> source) where T : class