简体   繁体   中英

Compare complex subquery between two values

I have repository with students and I want to get some students who have grades count in between 0 and 2.

This is my code:

_unitOfWork.Repository<Student>().Get(o => o.OrganizationId == organizationId
                && o.Grades.Where( o1 => o1.LastVersion
                        && o1.Type == 5
                        && o1.Value == 1).Count() > 0 
                && o.Grades.Where( o1 => o1.LastVersion
                        && o1.Type == 5
                        && o1.Value == 1).Count() <= 2 );

This code is working, but my question is how to change this query with less code.

Is there any way to replace Count with some variable and not use it two times in query?

something like this?

 var values = Enumerable.Range(1, 2);

 _unitOfWork.Repository<Student>().Get(o => o.OrganizationId == organizationId
            && values.Contains(
                o.Grades.Where( o1 => o1.LastVersion
                    && o1.Type == 5
                    && o1.Value == 1).Count()
               ));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM