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.
don’t start comparing languages that’s a big mistake, each one has its own shape and
LINQhas several divisions what you just do isLinq to objectthere is theLinq to SQL(and much more ...), need to give a read on the concepts.– novic
That one link you will already have to give a small vision or/and link
– novic