I have the following example of a program that implements the EF (Entity Framework). The bank and application structure is defined as follows:
Table Pessoa
:
- Primary key field:
id_pessoa
- Field name:
nome
- Field age:
idade
Class Pessoa
built from the% database% table by EF:
namespace EFPessoaExemplo
{
using System;
using System.Collections.Generic;
public partial class Pessoa
{
public int id_pessoa { get; set; }
public string nome { get; set; }
public int idade { get; set; }
}
}
And the method responsible for the Linq query:
private void lerPessoa(string nome, bool maiorIdade)
{
using (BancoEFEntities context = new BancoEFEntities())
{
IEnumerable<Pessoa> pessoa = from p in context.Pessoa
where p.nome.Contains(nome)
select p;
dataGridViewPessoa.DataSource = pessoa.ToList();
}
}
My question
In thepessoa
method, I would like to know if it is possible to change the query structure made in LINQ at runtime to append one more condition, which in this case is the second parameter of the lerPessoa(string nome, bool maiorIdade)
method, in case the value of the variable maiorIdade
is maiorIdade
the condition true
must be specified in the p.idade >= 18
clause.
Is there a way to do this without having to create an entire LINQ query for each condition?