如何编写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)
);
答案 0 :(得分:2)
不确定你在使用什么。
但是可以使用类级别来建议类似下面的东西。
假设您已将数据库中的所有数据加载到列表中。
var list= questionsList.Where(i => resultList.All(p => p.questionid != i.questionid));
答案 1 :(得分: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}
答案 2 :(得分: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.**