Search code examples
c#linq-to-sql

Can someone please explain what this LINQ query does?


I am having trouble understanding this LINQ query. This is what I think is happening.

1. from stat in XpoSession.Query<STUDENT>()
2. where stat.ID_PERSON.DT_BIRTH >= DateTime.Now.AddYears(-20)
3. && stat.Status.Where(val => val.CD_REASON == Constants.REASON_PLACED).Count() == 0
4. select stat.CD_STATUS.Trim()).ToList().GroupBy(val => val);

#1. Data source is STUDENT table from database.

#2. Gets all STUDENTS that are less than or equal to 20 years old.

#3. This is the part I don't understand. There is a STUDENT_STATUS table which has a CD_REASON column so I am assuming that stat.Status is really STUDENT_STATUS.CD_REASON and that gets passed into the lambda function and if it equals REASON_PLACED it does something with Count but not sure what.

#4. Groups by CD_STATUS value.


Solution

  • from stat in XpoSession.Query<STUDENT>()
    where stat.ID_PERSON.DT_BIRTH >= DateTime.Now.AddYears(-20)
    && stat.Status.Where(val => val.CD_REASON == Constants.REASON_PLACED).Count() == 0
    select stat.CD_STATUS.Trim()).ToList().GroupBy(val => val);
    

    Looks like it's getting whatever the CD_STATUS value is for students who are more than 20 years old where their status isn't whatever Constants.REASON_PLACED value is and grouping them by the CD_STATUS value.

    Without more context or additional code, it's not really possible to provide a better answer here.