I’m working on a ASP.NET MVC (an older version) application and I have a form where students are assigned an adviser based on a major code AND by alpha of a student’s last name.
I am trying to implement the below code but it is showing red scribbles to & operator:
Error: operator ‘&’ cannot be applied to operands of type ‘
System.Linq.IQueryable<Appointments.Models.MajorAdviserStudentAssignmentByAlpha>‘ and ‘lambda expression’ and ‘lambda expression’
var majorAssignmentByAlpha = FindAllMajorAdviserStudentAssignmentByAlphas()
.Where(
a => a.MajorString == student.StudentMajor) &
(a => ((String.Compare(student.StudentLastName, a.AlphaStart) >= 0) &
(String.Compare(student.StudentLastName, a.AlphaEnd) <= 0))
.FirstOrDefault());
if (majorAssignmentByAlpha != null)
return majorAssignmentByAlpha.Adviser;
I would appreciate any help to understand what’s wrong here.
Thanks in advance!
I would like to add a correction here: actually both
&&and&are boolean AND operators in C# (when used withbool). However the former will do a short-circuit evaluation:Consider
A && B, ifAisfalsethenBwill not be evaluated since the result will befalseno matter what is the truthness ofB.And for the error you are getting, probably that’s because your
&operator is comparing a boolean expression and a lambda expression (function). Try this