Pass List as expression parameter LINQ

3

I have a LINQ expression where the result is saved in the query variable, which is transformed into a list, this query returns a series of "RA" attributes. p>

I would like to pass this list of attributes as a parameter in the LINQ expression resulting in query2 . I want query2 to return all the discipline codes in which these "RA" are related (Yes, I want to pass all of them at once).

I did the form below but did not get results, the code compiled, but it did not work. How do I pass the query as a parameter in where ? Should I convert this list from 1 a single attribute to a common type? How do I?

void cargarDados(Int32 disc)
    {
       List<Turma> turmas = db.GetCollection<Turma>("turma").FindAll().ToList();

        var query = (from t in turmas
                     where t.COD_DISCIPLINA == disc
                     select new
                     {
                         t.RA                            

                     }).ToList();




        var query2 = (from t1 in turmas
                      where t1.COD_DISCIPLINA.Equals(query)
                      select new
                      {
                          t1.COD_DISCIPLINA
                      }).ToList();


        dataGridView1.DataSource = query2;

    }
    
asked by anonymous 04.11.2015 / 03:26

3 answers

2

You can do this;

protected void Page_Load(object sender, EventArgs e)
{
    cargarDados(1);
}

void cargarDados(Int32 disc)
{
    List<Turmas> turmas = new List<Turmas>();// db.GetCollection("turma").FindAll().ToList();
    Turmas turma = new Turmas();
    turma.RA = 1;
    turma.COD_DISCIPLINA = 1;
    turmas.Add(turma);

    Turmas turma1 = new Turmas();
    turma1.RA = 2;
    turma1.COD_DISCIPLINA = 1;
    turmas.Add(turma1);

//----------------------------------------------------------------//
    var RA = turmas
        .Where(t => t.COD_DISCIPLINA == disc)
        .Select(t => t.RA).ToList();

    var query2 = turmas
        .Where(t => RA.Contains(t.COD_DISCIPLINA))
         .Select(t => new
         {
             t.COD_DISCIPLINA,
         })
         .ToList();
//-------------------------------------------------------------//
} 
class Turmas
{
    public Int32 COD_DISCIPLINA { get; set; }
    public Int32 RA { get; set; }
}
    
04.11.2015 / 11:29
2

Note that when you make a new { t1.RA } , you are actually creating a new anonymous type that will not give you the desired result when comparing with the COD_DISCIPLINA property type.

As a final suggestion, I suggest doing everything in one query:

    var query2 = (from t1 in turmas
                  where (from t in turmas
                         where t.COD_DISCIPLINA == disc
                         select t.RA).Contains(t1.COD_DISCIPLINA)
                  select new
                  {
                      t1.COD_DISCIPLINA
                  }).ToList();
    
04.11.2015 / 11:51
1

Do this:

void cargarDados(Int32 disc)
        {
            List turmas = db.GetCollection("turma").FindAll().ToList();


        var query = (from t in turmas
                     where t.COD_DISCIPLINA == disc
                     select new
                     {
                         t.RA                            

                     }).ToList();




        var query2 = (from t1 in turmas
                      where query.Contains(t1.COD_DISCIPLINA)
                      select new
                      {
                          t1.COD_DISCIPLINA
                      }).ToList();


        dataGridView1.DataSource = query2;

    }
    
04.11.2015 / 03:38