smsware
smsware

Reputation: 479

LINQ's WHERE clause where COLUMN may be null

I have a problem with making WHERE clause with LINQ. I tried to google it but I've only got answers on what to do if the external variable is the type of nullable integer... well, I've tried these methods the other way around (I have 0...1 relations in my dataset): e.g.

int oldId = oldQuestion.id;
IEnumerable<possible_answer> possibleAnswersQuery =
            from x in Mentor11Entities.possible_answers
            where object.Equals(x.question_id, oldId)
            select x;
List<possible_answer> possibleAnswers = 
            possibleAnswersQuery.ToList<possible_answer>();

or

int oldId = oldQuestion.id;
IEnumerable<possible_answer> possibleAnswersQuery =
            from x in Mentor11Entities.possible_answers
            where Convert.ToInt32(x.question_id ?? 0).Equals(oldId)
            select x;
List<possible_answer> possibleAnswers = 
            possibleAnswersQuery.ToList<possible_answer>();

but I'm always getting the error that LINQ doesn't support certain functions... is there any way to get around the problem?

Upvotes: 0

Views: 1334

Answers (1)

Habib
Habib

Reputation: 223402

just use

where x.question_id != null && x.question_id == oldId

so your query should be:

IEnumerable<possible_answer> possibleAnswersQuery =
            from x in Mentor11Entities.possible_answers
            where x.question_id != null && x.question_id == oldId
            select x;

Upvotes: 4

Related Questions