简体   繁体   English

EFCore 查询和选择

[英]EFCore Query and selection

Hey I am trying to query and find a object that looks something like this:嘿,我正在尝试查询并找到一个看起来像这样的 object:

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();

What I am trying to do here is: find an object with id X, then that object with id X has a list of results, I want to get the list, BUT with only the one element that has id of result.Id.我在这里要做的是:找到一个 ID 为 X 的 object,然后那个 ID 为 X 的 object 有一个结果列表,我想得到这个列表,但只有一个元素的 ID 为 result.Id。

What am i doing wrong?我究竟做错了什么?

EDIT: Used this ATM编辑:使用这台 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;

Try this:尝试这个:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM