c# - Search max value and index inside Nested list -
i know can max value , index simple list in 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 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 max value of id have no idea how 2 indexes of it.
int maxvalue = emplist.selectmany(i => i.project).select(a => a.id).max();
the max value 24. want 2 indexes ( index 2 of employee , index 1 of project)
you can use overload of select
projects index create 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 employees have non-empty list<employee> project
)
Comments
Post a Comment