Pass List as LINQ expression parameter

Asked

Viewed 321 times

3

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

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

I did as below but I did not get results, the code compiled, but it did not work. As I pass the list 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;

    }

3 answers

2


You can do it like 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; }
}
  • 1

    Thanks Marconcilio, funfou, and with good performance ! Thanks

2

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

As a final suggestion, I suggest doing everything in a single 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();

1

Do it like 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;

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.