-1

I have a many-to-many between 'Edital' and 'Graduando' and I created a entity called 'EditalGraduando'. Now I want to know every 'edital' that 'graduando' subscribe using the graduando id. So I did this:

public IQueryable<int> GetGraduandosIds(int editalId)
{
    var graduandosId = db.EditalGraduandoe.Where(e => e.EditalId == editalId).Select(i => i.graduandoID);

    return graduandosId;
}

There's a way to do a select in the entety 'Graduando' using this result? Like this: SQL WHERE ID IN (id1, id2, ..., idn)

Community
  • 1
  • 1
Miss.Saturn
  • 155
  • 2
  • 13

1 Answers1

2

Yes, you can do this:

public IQueryable<Graduandoe> GetGraduandos(IEnumerable<int> graduandosIds)
{
    return db.Graduandoe.Where(g => graduandosIds.Contains(g.graduandoID));
}

Alternatively, you could have navigation properties, and you can write the following instead of the two functions:

public IQueryable<Graduandoe> GetGraduandos(int editalId)
{
    return db.EditalGraduandoe.Where(e => e.EditalId == editalId).Select(i => i.Graduandoe);
}
Rob
  • 26,989
  • 16
  • 82
  • 98
  • Do you know where can I find the documentation to this? – Miss.Saturn May 21 '16 at 16:46
  • How can I do join between entity Editals with the row Status from EditalGraduandoes (imgur.com/Ti4WUgR)? public IQueryable GetEditalsIds(int graduandoId) { return db.EditalGraduandoe.Where(e => e.graduandoID == graduandoId).Select(i => i.Edital); //with a join here } – Miss.Saturn May 21 '16 at 17:05
  • I found the documentation https://msdn.microsoft.com/en-us/library/system.linq.queryable(v=vs.100).aspx – Miss.Saturn May 21 '16 at 21:51
  • And I found good examples, including join. https://weblogs.asp.net/dixin/understanding-linq-to-sql-4-data-retrieving-via-query-methods – Miss.Saturn May 21 '16 at 22:19
  • and this is what I need it to get the Status too: public IQueryable GetEditalsIds(int graduandoId) { return db.EditalGraduandoe.Where(e => e.graduandoID == graduandoId).Select(edital => new { Edital = edital.Edital, Status = edital.Status}); } – Miss.Saturn May 21 '16 at 22:33