ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Find Minimum (Min) and Maximum (Max) value from Generic List of KeyValuePair using C# and VB.Net<p>Hey <a class="username" rel="bigbear">bigbear</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; var totals = new List&lt;KeyValuePair&lt;string, double&gt;&gt;(); &nbsp;&nbsp;&nbsp; totals.Add(new KeyValuePair&lt;string, double&gt;(&#34;mudassar&#34;, 1.5)); &nbsp;&nbsp;&nbsp; totals.Add(new KeyValuePair&lt;string, double&gt;(&#34;john&#34;, 5.8)); &nbsp;&nbsp;&nbsp; totals.Add(new KeyValuePair&lt;string, double&gt;(&#34;robert&#34;, 33.1)); &nbsp;&nbsp;&nbsp; totals.Add(new KeyValuePair&lt;string, double&gt;(&#34;test&#34;, 0.0)); &nbsp;&nbsp;&nbsp; double minValue = 0; &nbsp;&nbsp;&nbsp; double maxValue = 0; &nbsp;&nbsp;&nbsp; string maxKey = &#34;&#34;; &nbsp;&nbsp;&nbsp; string minKey = &#34;&#34;; &nbsp;&nbsp;&nbsp; foreach (KeyValuePair&lt;string, double&gt; item1 in totals) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (maxValue &lt; item1.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxKey = item1.Key; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxValue = item1.Value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; minValue = maxValue; &nbsp;&nbsp;&nbsp; foreach (KeyValuePair&lt;string, double&gt; item in totals) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (item.Value != 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (minValue &gt; item.Value) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minKey = item.Key; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minValue = item.Value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; Response.Write(minKey + &#34; Minimum value is :&nbsp; &#34; + minValue + &#34; &lt;br /&gt;&#34; + maxKey + &#34; Maximum Value is : &#34; + maxValue); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim totals = New List(Of KeyValuePair(Of String, Double))() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totals.Add(New KeyValuePair(Of String, Double)(&#34;mudassar&#34;, 1.5)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totals.Add(New KeyValuePair(Of String, Double)(&#34;john&#34;, 5.8)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totals.Add(New KeyValuePair(Of String, Double)(&#34;robert&#34;, 33.1)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totals.Add(New KeyValuePair(Of String, Double)(&#34;test&#34;, 0.0)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim minValue As Double = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim maxValue As Double = 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim maxKey As String = &#34;&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim minKey As String = &#34;&#34; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For Each item1 As KeyValuePair(Of String, Double) In totals &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If maxValue &lt; item1.Value Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxKey = item1.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxValue = item1.Value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minValue = maxValue &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For Each item As KeyValuePair(Of String, Double) In totals &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If item.Value &lt;&gt; 0 Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If minValue &gt; item.Value Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minKey = item.Key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minValue = item.Value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.Write(minKey &amp; &#34; Minimum value is :&nbsp; &#34; &amp; minValue &amp; &#34; &lt;br /&gt;&#34; &amp; maxKey &amp; &#34; Maximum Value is : &#34; &amp; maxValue) &nbsp;&nbsp;&nbsp; End Sub</pre> <p><span style="text-decoration: underline;"><strong>Output</strong></span></p> <p>mudassar Minimum value is : 1.5</p> <p>robert Maximum Value is : 33.1</p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/887108/Find-Minimum-Min-and-Maximum-Max-value-from-Generic-List-of-KeyValuePair-using-C-and-VBNet/https://www.aspforums.net:443/Threads/887108/Find-Minimum-Min-and-Maximum-Max-value-from-Generic-List-of-KeyValuePair-using-C-and-VBNet/Thu, 20 Dec 2018 03:00:38 GMT