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

1

What are the advantages of working with a fluent interface with LINQ?

I have this 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 query ( var consulta = from ) in SQL format and would like to know how to make this query leaner, with a fluent interface?

    
asked by anonymous 07.06.2016 / 19:29

3 answers

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;
}
    
07.06.2016 / 19:50
3

This query can be written more simply using more imperative form known as method syntax , as opposed to query syntax .

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

Here's how in dotNetFiddle and in CodingGround .

Some people find one easier to read than the other. This varies from who is reading and the type of query. The query syntax is often better suited for more complex expressions, with more than one data source ( from ) mostly by doing some sort of union ( join ). In addition, this form allows the use of auxiliary variables ( let ) .

    
07.06.2016 / 19:50
0

Query in SQL format:

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

Consultation in the Fluent Interface format:

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

Query in the Fluent Interface format, with Reverse Order:

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

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

var consulta = lista.OrderByDescending(n => n.Nome);
        .where(n => n.Nome.To.Lower().Contains("j"));
    
08.06.2016 / 21:30