7
I have the following example of a program that implements the EF (Entity Framework). The structure of the database and the application is defined as follows:
Table Pessoa
:
- Primary key field:
id_pessoa
- Name field:
nome
- Old field:
idade
Class Pessoa
constructed from the table pessoa
of the database by the RU:
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 consultation:
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 doubt
In the method lerPessoa(string nome, bool maiorIdade)
, i wonder if it is possible to change the structure of the LINQ query at runtime to add one more condition, which in this case is the second parameter of the method maiorIdade
, if the value of the variable maiorIdade
be it true
the condition p.idade >= 18
the clause shall be specified where
.
There is a way to do this without having to create an entire LINQ query for each condition?
It would be interesting for you to explain that
Where
, is not onlyLinq
and yesLambda Expression
withLinq
, only to be clearer to those who do not know. The solution itself is perfect.– Fernando Leal
Thanks Fernando, I’ll make this adjustment.
– Guilherme Guini