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