Use of LINQ to query a collection

Asked

Viewed 174 times

3

about min: I come from JAVA and recently I am learning c#, but I have a doubt about the use of LINQ. Many tutorials speak of the wonder that is LINQ but I so far can not understand its usefulness. 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;
}

Wouldn’t it be easier to do a direct database query? in JAVA I would do this using JDBC.

select * from tb_pessoas
where idade > 10

Maybe I’m just thinking about the database if that’s what gives me some other uses of LINQ.

  • don’t start comparing languages that’s a big mistake, each one has its own shape and LINQ has several divisions what you just do is Linq to object there is the Linq to SQL (and much more ...), need to give a read on the concepts.

  • That one link you will already have to give a small vision or/and link

2 answers

2

It would not be easier to make a direct consultation of the bank?

No, because LINQ is not only for formulating SQL queries. It is a much more powerful resource.

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

Suppose an object Cerveja:

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

Suppose a beer list:

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 },
};

I 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 real, 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 only perform a memory search operation. To go to the database, you would need to connect your code to a data source. The resource that does this is called LINQ to SQL. An example of full use is here.

Unlike pure LINQ, LINQ to SQL mounts a query through the accumulation of predicates in an object that implements IQueryable<T>, being T a class representing a database record in the . NET language you are using. This query is executed only when the code asks for the list to materialize (talk about it here).

LINQ to SQL received a first update, called LINQ to Entities, where the database to which the code connects is treated as a context of objects (ObjectContext) and each entity (table, in the case of a relational database) is mapped as a ObjectSet<TEntity>, being TEntity a class whose properties are the columns (or fields) of each data record.

This update has turned into a framework widely used currently called Entity Framework, more mature, optimized, which has additional capabilities to LINQ to Entities, such as the ability to generate a database through code and incremental support for data schema migrations. That is, if you are writing your code and modeling entities, the database tracks these changes in modeling through a resource known as migrations (or Migrations). We have a tag here on the site just talking of this framework.

0

Linq is a query abstraction language where it focuses on making it easier to write queries.

Utilize Linq aims to standardize all your queries made in c#, and this is its main feature, microsoft provides this content saying some reasons why use Linux not only Linq-to-sql, but as a data query standard.

Browser other questions tagged

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