2
What are the advantages of working with a fluent LINQ interface?
I have that code:
Employees.Cs.
namespace LinqConsulta
{
class Empregados : List<Empregado>
{
public Empregados Lista()
{
this.Add(new Empregado(1, "Maria", "[email protected]", "11 1111 1111"));
this.Add(new Empregado(2, "João", "[email protected]", "22 2222 2222"));
this.Add(new Empregado(3, "José", "[email protected]", "33 3333 3333"));
return this;
}
}
}
Employee.Cs
namespace LinqConsulta
{
class Empregado
{
public int Id { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Telefone { get; set; }
public Empregado() { }
public Empregado(int id, string nome, string email, string telefone)
{
this.Id = id;
this.Nome = nome;
this.Email = email;
this.Telefone = telefone;
}
}
}
Form1.Cs
namespace LinqConsulta
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_load(object sender, EventsArgs e)
{
Empregados lista = new Empregados().Lista();
var consulta = from empregado in lista
orderby empregado.Nome
select empregado;
dataGridView1.DataSource = consulta.ToList();
}
}
}
I’m working with the consultation(var consulta = from
) in SQL format and would like to know how to let this query more streamlined, with fluent interface?
Looks like you got two questions there, which one of you really wants to know?
– Maniero
What do you mean, "let the consultation get slower?"
– PauloHDSousa
I don’t know what your method is
Lista()
, but I’m pretty sure it’s possible to put the.OrderBy()
on it, thus not needing the query variable (var consulta = ...
).– Randrade
So you don’t need the query variable, you can put the
.OrderBy()
directly on the list, thus:Empregados lista = new Empregados().Lista().OrderBy(n => n.Nome);
.– Randrade
Is this form only for study? For there are better ways to do this.
– Randrade
@Paulohenriqueneryoliveira Then I won’t even put an answer. But my above comment will leave the code in "one line", if that’s what you mean as clean.
– Randrade