0

我一直在阅读有关 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 风格。

系统可以为用户分配问题(问题由系统即 admin 生成,最初不属于任何人)。之后,可以将相同的问题分配给多个不同的用户。然后每个用户都可以回答问题。

该问题包含一个给出的答案列表。

因此,例如,如果将问题 1 分配给用户 1 到 10,但只有用户 2 和 4 回答,则该问题在“答案列表”中将只有 2 个答案。

我的目标是能够对数据源进行一些基本的排序和选择。

例如:

  1. 对于给定用户 - UserA -,返回 UserA回答该问题的所有问题。

  2. 对于系统中的所有问题,返回分配给该问题但该用户回答/未回答的所有用户

  3. 为用户选择在给定日期之后被/未被回答的问题和答案

4

2 回答 2

0

I haven't tested them, but I think it should give you what you want.

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();
于 2013-02-04T21:56:05.650 回答
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();
}
于 2013-02-04T22:00:36.970 回答