c# - Use LINQ to retrieve largest number from Properties of a collection, within a collection -


here's model:

public class seasonscore {     [key]     public int seasonscoreid { get; set; }      [displayname("season")]     public string season { get; set; }      [displayname("year")]     public int year { get; set; }      [displayname("value")]     public decimal value { get; set; }      [displayname("subject")]     public virtual subject subject { get; set; } }  public class subject {     [key]     public int subjectid { get; set; }      [displayname("subject")]     public string subject { get; set; }      [displayname("scores")]     public virtual list<seasonscore> scores { get; set; }      public subject()     {         scores = new list<seasonscore>();     } } 

as can see each instance of "subject" contains list of "seasonscore". can see seasonscore has decimal property called "value".

i have database stores list of subject objects. want find largest "value" property of seasonscore instances contained within of subject instances in database.

i long way round, i'm convinced should able using linq although can't quite figure out.

you can use enumerable.max function. has overload takes selector. after list of maximum values each subject, call max function again largest of values.

subjects.select(x => x.scores.max(y => y.value)).max(); 

see in action here.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -