EFCore查询和选择

时间:2020-06-10 09:07:12

标签: c# .net-core ef-core-3.1

嘿,我试图查询并找到一个看起来像这样的对象:

I have already result 
I have id that is quiz id.

So i have both Ids.


var singlequiz = await _context.Quizzes
                .Include(q => q.Questions)
                    .ThenInclude(question => question.Options)
                .Include(qz => qz.Results)
                    .Where(b => b.Id == id)
                .Select(c => c.Results.Select(z => z.Id == result.Id))
                .FirstOrDefaultAsync();

我在这里想要做的是:找到一个ID为X的对象,然后该ID为X的对象有一个结果列表,我想得到这个列表,但只有一个具有结果ID的元素。 ID。

我在做什么错了?

编辑: 使用了这个ATM

 var singlequiz = await _context.Quizzes
                .Include(x => x.Questions)
                    .ThenInclude(x => x.Options)
                .Include(x => x.Results)
                .FirstOrDefaultAsync(x => x.Id == id);

            var singleQuizElement = singlequiz.Results.Where(x => x.Id == result.Id).ToList();


            singlequiz.Results = singleQuizElement;

1 个答案:

答案 0 :(得分:0)

尝试一下:

var singlequiz = await _context.Quizzes
                .Include(x => x.Questions)
                    .ThenInclude(x => x.Options)
                .Include(x => x.Results)
.FirstOrDefaultAsync(x=> x.Id == id && x.Results.Any(y => y.Id == result.Id));