1

How to write a linq query to retreive questions from questions table which is not attempt by user before.

question table

create table questions
(
  questionID int,
  question varchar,
  primary key(questionID)
);

create table result
{
  id int,
  answered_by varchar,
  questionid int,
  answer varchar,
  primary key (id),
  foreign key ( questionid) references question(questionID)
);
Kaf
  • 33,101
  • 7
  • 58
  • 78

3 Answers3

2

Not sure what are you using.

But can suggest something like below using class level.

Assuming you have load all the data from database to to lists.

var list= questionsList.Where(i => resultList.All(p => p.questionid != i.questionid));
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
0
from QUESTION in QUESTIONS
join
ANSWER in RESULT
on
QUESTION.questionID equlas ANSWER.questionid
where
ANSWER.answer!=NULL
select {QUESTION.questionID, ANSWER.answered_by, ANSWER.questionid, ANSWER.answer}
huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Kashif Junaid
  • 21
  • 1
  • 5
0

For people who start with a group of in-memory objects and are querying against a database, I've found this to be the best way to go:

var itemIds = resultList.Select(x => x.questionid ).ToArray();
var results = questionsList.Where(x => !itemIds.Contains(x.questionid ));

**This produces a nice WHERE ... IN (...) clause in SQL.**
Thilina H
  • 5,754
  • 6
  • 26
  • 56
  • I think you can do the same like this with single line `var x = questionsList.Where(i => !resultList.Any(p => p.questionid == i.questionid ));` – huMpty duMpty Sep 25 '13 at 12:09