What are the advantages of working with Fluent Interface with LINQ?

Asked

Viewed 216 times

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?

  • What do you mean, "let the consultation get slower?"

  • 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 = ...).

  • 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);.

  • Is this form only for study? For there are better ways to do this.

  • @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.

Show 1 more comment

3 answers

4

This query can be written in a simpler way using the the most imperative form known to syntax method, as opposed to query syntax.

var consulta = lista.OrderBy(e => e.Nome);

Behold functioning in a simplified way in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Some people find one easier to read than the other. This varies from who is reading and the type of query. Query syntax is usually best suited for more complex expressions, with more than one data source (from) mainly doing some kind of data union (Join). In addition this form allows the use of auxiliary variables (Let).

3

Take a look like this.

private void Form1_load(object sender, EventsArgs e) 
{
    Empregados lista = new Empregados().Lista(); 

    var consulta = lista.OrderBy(p => p.Nome).ToList();

    dataGridView1.DataSource = consulta;
}

0

Query in SQL format:

var consulta = from empregado in lista
               orderby empregado.Nome
               select empregado;

Query in Fluent Interface format:

var consulta = lista.OrderBy(n => n.Nome);

Query in Fluent Interface format, with Inverse Order:

var consulta = lista.OrderByDescending(n => n.Nome);

Query in Fluent Interface format, with Inverse Order and with search where Name contains the letter "J" :

var consulta = lista.OrderByDescending(n => n.Nome);
        .where(n => n.Nome.To.Lower().Contains("j"));

Browser other questions tagged

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