我一直在阅读有关LINQ查询的文档和教程,但似乎无法做到正确。我有一个类似于这个的模型,我正在使用实体框架来坚持它:
public class Question
{
public int Id {set;get;}
public string Question {set;get;}
public DateTime DateCreated {get;set;}
public List<Answer> Answers {set; get;}
}
public class Answer
{
public int Id {set; get;}
public string UserAnswer {get;set;}
public DateTime DateAnswered {get;set;}
public User TheUserWhoAnswerd {get;set;}
}
public class User
{
public int Id { get;set;}
public string UserName {get;set;}
public DateTime DateCreated {get;set;}
public List<Question> Questions { get;set;}
}
The data context has something like this
public HashSet<Question> AllOfTheQuestionsDb {get;set;}
public HashSet<Answer> AllOfTheAnswersDb {get;set;}
public HashSet<User> AllOfTheUsersDb {get;set;}
我的流程类似于堆栈溢出--trivia风格。
系统可以向用户分配问题(问题由系统生成,即管理员,最初不属于任何人)。之后,可以将相同的问题分配给多个不同的用户。然后每个用户都可以回答这个问题。
这个问题列出了给出的答案。
因此,例如,如果问题1被分配给用户1到10,但只有用户2和4被回答,则该问题在“答案列表”中只有2个答案。
我的目标是能够做一些基本的排序并选择数据源。
例如:
对于给定用户 - UserA - ,返回UserA 未回答该问题的所有问题。
对于系统中的所有问题,返回问题已分配给该用户的所有用户但该用户确实/未回答
选择在给定日期之后未被回答的用户的问题和答案
答案 0 :(得分:0)
我没有对它们进行过测试,但我认为它应该能满足您的需求。
int UserA = 1;
// For a given user - UserA -, return all questions where UserA did not answer that question
// Look for questions where the count of UserA appearances is zero.
List<Question> NotAnsweredByUserA = AllOfTheQuestionsDb.Where(a => a.Answers.Count(b => b.TheUserWhoAnswerd.Id == UserA) == 0).ToList();
// For all questions in system, return all users that the question was assigned to but that user did/didn't answer
// Look for answers in questions where a User has been assigned, but no answer, and select that user
List<User> NotAnsweredButAssigned = AllOfTheQuestionsDb.SelectMany(a => a.Answers.Where(b => b.TheUserWhoAnswerd != null && string.IsNullOrWhiteSpace(b.UserAnswer)).Select(c => c.TheUserWhoAnswerd)).ToList();
// Selecting questions and answers for a user that were not answered after a given date
// Select the questions where the answer contains an answer date before today and answered by UserA
List<Question> NotAnsweredBeforeDate = AllOfTheQuestionsDb.Where(a=>a.Answers.Any(b=>b.DateAnswered < DateTime.Now && b.TheUserWhoAnswerd.Id == UserA)).ToList();
答案 1 :(得分:0)
这应该让你接近或给你一个很好的起点:
List<Question> QuestionsUnasweredByUser(User givenUser)
{
return AllOfTheQuestionsDb
.Where(q => q.Answers
.Any(a => AllOfTheAnswersDb
.Where(na => na.TheUserWhoAnswerd.Id != givenUser.Id)
.ToList().Contains(a)))
.ToList();
}
List<User> UserAssignedQuestions(bool answered)
{
return AllOfTheUsersDb
.Where(u => u.Questions.Count > 0 &&
answered ? u.Questions.Any(q=> q.Answers.Count > 0)
: u.Questions.Any(q=>q.Answers.Count == 0)).ToList();
}
List<Question> OverdueQuestions(User givenUser, DateTime dueDate, bool answered)
{
return AllOfTheQuestionsDb
.Where(q => (q.Answers.Count == 0 && !answered) ||
q.Answers.Any(a => a.TheUserWhoAnswerd.Id == givenUser.Id &&
answered ? a.DateAnswered > dueDate
: a.DateAnswered <= dueDate)).ToList();
}
List<Answer> OverdueAnswers(User givenUser, DateTime dueDate, bool answered)
{
return AllOfTheAnswersDb
.Where(a => a.TheUserWhoAnswerd.Id == givenUser.Id &&
answered ? a.DateAnswered < dueDate
: a.DateAnswered <= dueDate).ToList();
}