简体   繁体   English

结果实体框架5中的LINQ子查询

[英]LINQ Sub Query In Result Entity Framework 5

I've the following class 我上以下课

public class Interview
{
    public int Id { get; set; }
    public ICollection<InterviewSlot> Slots { get; set; }
}

public class InterviewSlots
{
    public int Id { get; set; }
    public Candidate Candidate { get; set; }
}

public class Candidate
{
    public int Id { get; set; }
}

I want something like this, 我想要这样的东西

var candidates = _DbContext.Interviews.Where(i => i.Id == Id).Select(s => s.Slots.Select(c => c.Candidate).ToList();

I don't want to use the InterviewSlots or the Candidate object 我不想使用InterviewSlots或Candidate对象

I want to get all the Candidates in a interview. 我想让所有候选人面试。

What would the LINQ be for this?? LINQ会做什么呢?

I'm thinking it may be along the lines of something like this in linq: 我认为这可能与linq中类似的内容类似:

var candidates = _DbContext.Interviews.Where(i => i.Id == id)
    .SelectMany(interview => interview.Slots)
    .Select(slot => slot.Candidate)
    .ToList();

tho, without seeing exactly how you plan to use it, quite a tricky one to answer. 因此,在没有确切了解您打算如何使用它的情况下,很难回答。

I don't really understand your question 我不太明白你的问题

What would the LINQ be for this?? LINQ会做什么呢?

But here's what you need in order to get all candidates in an interview. 但是,这是让所有候选人都面试的条件。

Without null checking. 没有空检查。

var interview = _DbContext.Interviews.Where(i => i.Id == Id).Single();
var candidates = interview.Slots.Select(s => s.Candidate);

With null checking 空检查

var interview = _DbContext.Interviews.Where(i => i.Id == Id).SingleOrDefault();
if (interview != null)
    var candidates = interview.Slots.Select(s => s.Candidate);

In one line 一行

_DbContext.Interviews.Where(i => i.Id == Id)
                     .Single()
                        .Slots.Select(s => s.Candidate);

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

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