Using LINQ to query a collection

3

about min: I come from JAVA and recently I'm learning C #, but I have a question about using LINQ. Many tutorials speak of the wonder that is LINQ but I can not understand its usefulness so far. Look at this code below:

    class Program
{
    static void Main(string[] args)
    {
        Pessoa p1 = new Pessoa() { nome= "Fulano", idade = 10};
        Pessoa p2 = new Pessoa() { nome = "Fulano2", idade = 18 };

        List<Pessoa> listaDePessoa = new List<Pessoa>();
        listaDePessoa.Add(p1);
        listaDePessoa.Add(p2);

        var consulta = from p in listaDePessoa
              where p.idade > 10
              select p;

    }

}

public class Pessoa
{
    public string nome;
    public int idade;
}

Would not it be easier to do a direct consultation of the bank? in JAVA I would do this using JDBC.

select * from tb_pessoas
where idade > 10

Maybe I'm just thinking about the database if it gives me some other uses of LINQ.

    
asked by anonymous 12.12.2016 / 20:14

2 answers

2
  

Would not it be easier to do a direct consultation of the bank?

No, because LINQ is not just for SQL querying. It's a much more powerful feature.

LINQ, as per Wikipedia article , "is a component of Microsoft .NET that adds query functionality in some languages of .NET programming. " This means that we can use this query functionality on any .NET object in any programming language whose code is managed ( managed ).

Assume an object Cerveja :

public class Cerveja 
{
    public String Nome { get; set; }
    public String Tipo { get; set; }
    public decimal Preco { get; set; }
}

Suppose a list of beers:

var cervejas = new List<Cerveja> 
{
    new Cerveja { Nome = "Budweiser", Tipo = "American Lager", Preco = 3M },
    new Cerveja { Nome = "Heineken", Tipo = "Pale Lager", Preco = 3.5M },
    new Cerveja { Nome = "Stella Artois", Tipo = "Pilsner", Preco = 3.3M },
};

Add the LINQ feature to my code:

using System.Linq;

I can search the list using a query syntax. For example, if I want to know if there is a beer with the name "Budweiser":

var bud = cervejas.FirstOrDefault(c => c.Nome == "Budweiser");

The search occurs in memory and returns an object. If there was no beer whose name is "Budweiser" in the list, the return would be a null object.

If I want all the beers more expensive than 3 reais, I can use:

var cervejasCaras = cervejas.Where(c => c.Preco > 3M).ToList();

About your example

This example does not go to the database. Just like in my example, you just do an in-memory lookup operation. To go to the database, you would need to connect your code to a data source. The feature that does this is called LINQ to SQL . An example of full use is here .

Unlike pure LINQ, LINQ to SQL assembles a query by accumulating predicates on an object that implements IQueryable<T> ", being T a class that represents a database record in the .NET language that you are using. This query is executed only at the time the code asks for the materialization of the list ( I mention this here ).

LINQ to SQL received a first update, called LINQ to Entities , where the database to which the code connects is treated as an object context ( ObjectContext ") and each entity (table, in the case of a relational database) is mapped as an

13.12.2016 / 02:55