1

如何编写 linq 查询以从问题表中检索用户之前未尝试的问题。

问题表

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)
);
4

3 回答 3

2

不知道你在用什么。

但可以使用类级别建议如下内容。

假设您已将所有数据从数据库加载到列表。

var list= questionsList.Where(i => resultList.All(p => p.questionid != i.questionid));
于 2013-09-25T11:32:02.607 回答
0

对于从一组内存对象开始并查询数据库的人,我发现这是最好的方法:

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.**
于 2013-09-25T11:53:42.130 回答
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}
于 2013-09-25T11:34:32.890 回答