Isolet Chan
Isolet Chan

Reputation: 427

Search max value and index inside Nested list

I know I can get the max value and index from a simple list in this way

List<Employee> emplist = new List<Employee>()
                        {
                         new Employee{Age=15, name = "Tom"},
                         new Employee{Age=17, name = "Billy"},
                         new Employee{Age=25, name = "Sam"}
                        };

int maxvalue = emplist.Select(i => i.Age).Max();
int index = empList.FindIndex(t => t.Age == maxvalue);

But for a nested list

List<Employee> emplist = new List<Employee>()
                    {
                     new Employee{Age=15, name = "Tom", new List<project>
                     {
                        ID = 12, name = "Project A",
                        ID = 11, name = "Project B",
                        ID = 16, name = "Project C"
                     }},
                     new Employee{Age=17, name = "Billy",new List<project>
                     {
                        ID = 17, name = "Project D",
                        ID = 18, name = "Project E",
                        ID = 10, name = "Project F"
                     }},
                     new Employee{Age=25, name = "Sam",new List<project>
                     {
                        ID = 22, name = "Project X",
                        ID = 24, name = "Project Y",
                        ID = 19, name = "Project Z"
                     }}
                    };

I know how to get the max value of ID but have no idea how to get the two indexes of it.

int maxvalue = emplist.SelectMany(i => i.project).Select(a => a.ID).Max();

The max value is 24. I want to get the two indexes as well ( Index 2 of employee and Index 1 of project)

Upvotes: 1

Views: 1312

Answers (3)

WiiMaxx
WiiMaxx

Reputation: 5420

this would be do the same without odering your projectlist:

    var employee    = emplist.Aggregate((e1, e2) => (e1.project.Max(p1 => p1.ID) > e2.project.Max(p2 => p2.ID)) ? e1 : e2);
    var employeeidx = emplist.IndexOf(employee);
    var project     = employee.project.Aggregate((p1, p2) => (p1.ID > p2.ID) ? p1 : p2);
    var projectidx  = employee.project.IndexOf(project);
    var value       = project.ID;

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460028

You can use the overload of Select which projects also the index to create an anonymous type:

var maxItem = emplist
   .Select((emp, index) => new 
    { 
        maxProject = emp.project
            .Select((proj, pIndex) => new{ proj, pIndex })
            .OrderByDescending(x => x.proj.ID)
            .First(),
        emp, index 
    })
    .OrderByDescending(x => x.maxProject.proj.ID)
    .First();
Console.Write("Max-Value:{0} Emp-Index:{1} Project-Index:{2}"
    , maxItem.maxProject.proj.ID
    , maxItem.index 
    , maxItem.maxProject.pIndex);

(disclaimer: untested, presumes that all employees have a non-empty List<Employee> project)

Upvotes: 2

Bob Vale
Bob Vale

Reputation: 18474

Try the following Linq

var result =  emplist.Select((x,i) => new { index = i, item = x})
                     .SelectMany(x => x.item.project.Select(
                          (a,i) => new { index = x.index, subindex = i, id = a.ID}))
                     .OrderByDescending(x => x.id )
                     .First();
  • First index will be in result.index = 2
  • Second index will be in result.subindex = 1
  • ID will be in result.id = 24

Upvotes: 3

Related Questions