- public class Prodotti
- {
- public string Id{ get; set; }
- public string Descrizione{ get; set; }
- public int Quantita{ get; set; }
- public double Prezzo{ get; set; }
- }
This class is used to enter the necessary data, after which you create a Collection type Products at class level.
- 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.
- prodotti.Add(new Prodotti{ Id = "1", Descrizione = "prodotto 1", Quantita = 12, Prezzo = 12.50 });
- prodotti.Add(new Prodotti{ Id = "1", Descrizione = "prodotto 2", Quantita = 1, Prezzo = 1.50 });
- prodotti.Add(new Prodotti{ Id = "3", Descrizione = "prodotto 3", Quantita = 23, Prezzo = 2.50 });
- 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.
- 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:
- public class Dettagli
- {
- public int Quantita{ get; set; }
- public double Prezzo{ get; set; }
- }
Finally, a simple LINQ query where, based on the value of the field, It'll display the quantity and the price.
- private void button1_Click(object sender, EventArgs e)
- {
- var result = from a in prodotti
- where a.Id == "1"
- select new Dettagli{ Quantita = a.Quantita, Prezzo = a.Prezzo };
- dataGridView2.DataSource = result.ToList();
- }
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.

NitinPosted May 11, 2015, 12:15 PM
nice
Former memberPosted May 11, 2015, 9:24 AM
Nice start, keep it up
Sibeesh VenuPosted May 11, 2015, 3:00 AM
Good One :)