LINQ To Objects View Data According to a Search Criteria In C#

This article shows how to perform searches with LinqToObjects within a collection for the accuracy of the lists of objects. The collection, by implementing IEnumerable, provides the ability to perform searches in them by the query, all this using Linq To Objects, where you can write queries to search and see what interests us under a certain condition. First, start by creating a class with the four properties, part number, description, quantity and price. We will simulate the insertion of an order placed by a client and stored for simplicity as mentioned in a list of type products.
  1. public class Prodotti  
  2. {  
  3.     public string Id{ getset; }  
  4.     public string Descrizione{ getset; }  
  5.     public int Quantita{ getset; }  
  6.     public double Prezzo{ getset; }  
  7. }  

This class is used to enter the necessary data, after which you create a Collection type Products at class level.

  1. private List<Prodotti> prodotti = new List<Prodotti>();  

The we value in this way, I used fixed values but nothing prevents exploiting the properties of the Class Products otherwise.

  1. prodotti.Add(new Prodotti{ Id = "1", Descrizione = "prodotto 1", Quantita = 12, Prezzo = 12.50 });  
  2. prodotti.Add(new Prodotti{ Id = "1", Descrizione = "prodotto 2", Quantita = 1, Prezzo = 1.50 });  
  3. prodotti.Add(new Prodotti{ Id = "3", Descrizione = "prodotto 3", Quantita = 23, Prezzo = 2.50 });  
  4. prodotti.Add(new Prodotti{ Id = "4", Descrizione = "prodotto 4", Quantita = 45, Prezzo = 42.50 });  

Use the following procedure to display it, for example in a DataGrid control by implementing the DataSource property.

  1. dataGridView1.DataSource = prodotti.ToArray();  

This line of code will display in the DataGrid control all the content of the Collection Products. So far, we have displayed all list data products in the DataGrid control, now comes the less simple, how to display the products on the basis of a selection. For example, view the data quantity and the price of all the products that year equal to Id 1. Again we should create a class that will allow you to view data of products, for example:

  1. public class Dettagli  
  2. {  
  3.     public int Quantita{ getset; }  
  4.     public double Prezzo{ getset; }  
  5. }  

Finally, a simple LINQ query where, based on the value of the field, It'll display the quantity and the price.

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     var result = from a in prodotti  
  4.     where a.Id == "1"  
  5.         select new Dettagli{ Quantita = a.Quantita, Prezzo = a.Prezzo };  
  6.     dataGridView2.DataSource = result.ToList();  
  7. }  

After the execution of the search queries we thus get all orders executed by Id order equal to 1. LinqToObjects is not limited to keyword or Select Where used in this example, but it has many keywords, for more information on MSDN Library examples are explained in detail.


Similar Articles