我有一个答案实体列表(列表大小为50-2000个项目 - 实体具有比此处所示更多的属性) 我想要一个linq查询来为那些尚未得到正确答案的人选择所有实体 所以我的实体输出列表将通过fred和harry从下表中排除所有实体,因为它们每个都有正确的答案 - 并且包括所有带有bob和bill的答案的实体 - 我必须在isCorrect上选择而不是在答案本身上当呈现给用户时,答案被扰乱,我也需要完整的实体。
- username | answer | isCorrect | .....
- fred | a | false exclude as he has 1 correct
- fred | d | false exclude as he has 1 correct
- fred | b | false exclude as he has 1 correct
- fred | a | true exclude as he has 1 correct
+ bob | a | false need this entity
+ bob | b | false need this entity
+ bill | a | false need this entity
+ bill | b | false need this entity
- harry | a | false exclude as he has 1 correct
- harry | b | true exclude as he has 1 correct
- harry | c | false exclude as he has 1 correct
我可以获得没有正确答案的用户列表
var usersWithNoCorrectAnswers = entities
.GroupBy(a => a.UserName)
.Where(g => g.Any(x => !x.IsCorrect))
.Select(g => g.Key)
.ToList();
但我不确定如何获得没有正确答案的实体列表 感谢
答案 0 :(得分:2)
要让获得所有答案错误的用户获得所有实体,您可以对用户名进行分组并筛选出任何正确的用户。然后只需选择组的实体并展平结果。
var results = entities.GroupBy(a => a.UserName(
.Where(g => !g.Any(a => a.IsCorrect))
.SelectMany(g => g)
.ToList();
答案 1 :(得分:1)
我认为你需要这样的东西:
var results = entities.GroupBy(a => a.UserName(
.Where(g => !g.Any(a => a.IsCorrect))
.SelectMany(g => g.Select(a =>
new {
username = a.username,
answer = a.answer,
...
}
))
.Distinct()
.ToList();
或者
var results = entities.GroupBy(a => a.UserName(
.Where(g => !g.Any(a => a.IsCorrect))
.SelectMany(g => g.Select(a => a))
.Distinct()
.ToList();
答案 2 :(得分:1)
但我不确定如何获得没有正确答案的实体列表
var noCorrectAnswers = entities
.GroupBy(q => q.UserName)
.Where(g => !g.Any(a => a.IsCorrect))
.SelectMany(g => g)
.ToList();