从另一个集合中筛选数据集
本文关键字:筛选 数据集 集合 另一个 | 更新日期: 2023-09-27 17:57:52
我一直在阅读关于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;}
我的流类似于堆栈溢出——琐事风格。
系统可以将问题分配给用户(问题由系统生成,即管理员,最初不属于任何人)。之后,相同的问题可以分配给多个不同的用户。然后每个用户都可以回答该问题。
这个问题有一个答案列表
因此,例如,如果问题1分配给用户1到10,但只有用户2和4回答,则该问题在"答案列表"中只有2个答案。
我的目标是能够对数据源进行一些基本的排序和选择。
例如:
对于给定的用户(UserA-),返回UserA没有回答该问题的所有问题。
对于系统中的所有问题,返回问题分配给但该用户回答了/没有回答的所有用户
为用户选择在给定日期之后未回答的问题和答案
我还没有测试过它们,但我认为它应该能满足您的需求。
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();
这应该会让你接近或给你一个好的起点:
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();
}