Posts

Showing posts with the label Dictionary

Donate

How To Iterate Or Loop Through A C# Dictionary And Update It's Value

Hello, Most of us C# developers are working on Dictionaries to store multiple sets of data with unique keys. And in times that we need to modify our logic with the Dictionary object involved, we normally do that using looping construct. Given a simple Dictionary object below with int as key and string as value. Dictionary< int , string > styledLines = new Dictionary< int , string >(); We may thought that looping through this object with the code presented below may work. However, when running your app, this will cause a "Collection was modified; enumeration operation may not execute" issue. It's because we are using the collection's key directly. foreach ( var kvp in styledLines) { styledLines[kvp.key] = $ "Series# {seriesNum} "; } To fix that we have a couple of options. Given that our key is an integer type with values in sequential order such as 0...1000, we could use for loop to change the values such as below. var startI

ASP.NET MVC ListBoxFor() With Optgroup Tag Support

Image
Here's a simple ListBoxFor helper that supports optgroup tag. See versions for C# and VB.NET. C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public static class HtmlExtensions { public static IHtmlString ListBoxFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Dictionary< string , IEnumerable<SelectListItem>> selectList, object htmlAttributes = null ) { var select = new TagBuilder( "select" ); select .Attributes.Add( "name" , ExpressionHelper.GetExpressionText(expression)); if (htmlAttributes != null ) { RouteValueDictionary routeValues = new RouteValueDictionary(htmlAttributes); if (!routeValues.ContainsKey(( "size" ).ToLower(

Sorting A HashTable Object Using LINQ In C#.NET And VB.NET

Image
In .NET, you can't directly sort a Hashtable object. A tip on sorting a Hashtable object is to cast it to Dictionary then apply LINQ OrderBy() method to the Hashtable object. See examples below: VB.NET 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Private Sub SortHashTableKey () hash = New Hashtable() hash.Add( "B" , 1 ) hash.Add( "A" , 2 ) hash.Add( "C" , 3 ) Dim dict = hash.Cast( Of DictionaryEntry)() _ .ToDictionary(Function(d) d.Key, Function(d) d.Value) _ .OrderBy(Function(e) e.Key) Console.WriteLine( "Sort by Key" ) For Each item In dict.ToList() Console.WriteLine( String .Format( "{0}, {1}" , item.Key, item.Value)) Next Console.WriteLine(Environment.NewLine) End Sub Private Sub SortHashTableValue ()

How To Sort Dictionary<TKey,TValue> Object By Value Or By Key Using LINQ In C#

Here's a code I got from StackOverflow on how to sort dictionary object by value or by key using LINQ in C#. var sortedDict = ( from entry in SortedWebsiteNames orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value); var sortedDict = ( from entry in SortedWebsiteNames orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

Donate