Click here to Skip to main content
16,018,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have declared following dictionary-

C#
 Dictionary<DateTime, List<Employee>> dictSource = new Dictionary<DateTime, List<Employee>>
{
{ DateTime.Now.AddDays(1), new List<Employee> { 1, "Programmers" }},
{ DateTime.Now.AddDays(3), new List<Employee> { 90, "Testers" }},
{ DateTime.Now.AddDays(2), new List<Employee> { 1, "Designers" }},
};


Then , I am extracting the records having minimum value of element at zero postion.
C#
int MinValue = dictSource.Min(pair => pair.Value.ElementAt(0));
var filteredresults = dictSource.Where(pair => pair.Value.ElementAt(0) == MinValue);

Above code returns 2 items-
1, "Programmers"
1, "Designers"

Question, I need to drill the 'filteredresults' collection so that it returns item with minimum date. So, I should get the '1, "Programmers"' as final value.

I have tried following but failing-
C#
filteredresults = filteredresults.ToDictionary(s => s.Key, s.Value);


Please help here?
Posted
Comments
Tomas Takac 15-Jun-15 3:23am    
I'm confused. You have list of employees and you are inserting integers and strings there. Shouldn't these be properties of the Employee object instead?

I have found the solution. It is-

C#
int MinValue = dictSource.Min(pair =&gt; pair.Value.ElementAt(0));
var filteredresults = dictSource.Where(pair =&gt; pair.Value.ElementAt(0) == MinValue);

filteredresults = filteredresults.OrderBy(p =&amp;gt; p.Key);
var finalResult = filteredresults.First();
lstvalueGet = finalResult.Value;
 
Share this answer
 
Please check the below code :-
C#
Dictionary<datetime,>> dictSource = new Dictionary<datetime,>> 
{ 
    { DateTime.Now.AddDays(1), new List<employee>{ new Employee{ EmpID= 1, Designation= "Programmers" } }},
    { DateTime.Now.AddDays(3), new List<employee>{ new Employee{ EmpID= 90, Designation= "Testers" } }},
    { DateTime.Now.AddDays(2), new List<employee>{ new Employee{ EmpID= 1, Designation= "Designers" } }} 
};

int MinValue = dictSource.Min(Pair=>Pair.Value.ElementAt(0).EmpID);
DateTime Mindt= dictSource.Min(Pair => Pair.Key);


var filteredresults = dictSource.Where(Pair => Pair.Value.ElementAt(0).EmpID  == MinValue);
         

Response.Write(" Min Value = " + MinValue);
 

Response.Write("<br>" + "<br>" + " Filtered list having min value :-  ");


foreach (var item in filteredresults)
{ 
    Response.Write("<br>" + " Designation at MinValue= " + item.Value.ElementAt(0).Designation);
}

filteredresults = dictSource.Where(Pair => Pair.Key == Mindt);
         
Response.Write("<br>" + "<br>" + " Min Date = " + Mindt);


Response.Write("<br>" + "<br>" + " Filtered list having min date :-  ");

foreach (var item in filteredresults)
{
    Response.Write("<br>" + " Date at Min Date = " + item.Value.ElementAt(0).Designation );
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900