DevS
DevS

Reputation: 123

Operator '==' cannot be applied to operands of type 'int' and 'IQueryable<int>'

I am getting error in my "where" condition. Below is my code:

var elimentid = db.Consult.Where(x => x.EnquiryId == Patientid).Select(x => x.AilmentId);
var details = (from detail in db.M_SelfHelp
                join alimet in db.Aliment on detail.SubAilmentId equals alimet.AilmentId
                where (detail.AilmentId == elimentid)
                select new SelpHelp
                {
                    AlimemtName = alimet.Name,
                    Alimentid = alimet.AilmentId
                }
                ).ToList();

Please let me know whats wrong in my code ?

Upvotes: 0

Views: 93

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You need to materialize you first query to singular result :

var elimentid = db.Consult.Where(x => x.EnquiryId == Patientid)
                          .Select(x => x.AilmentId).FirstOrDefault();

or you can write:

var consult = db.Consult.Where(x => x.EnquiryId == Patientid).FirstOrDefault();
var elimentid = consult.AilmentId;

Right now it is not and int but IQueryable<int> and you can't compare int with IQueryable<int> obviously using == operator.

Or you can also use SingleOrDefault() if we are sure that there will always be single record agianst the EnquiryId in Consult.

This would make your code compile and workable as well but might not give you expected results back from linq query, as if there are multiple rows expected from the first query and you want to put all rows against those ids, then you need to do some work.

Upvotes: 3

Related Questions