Tag Archives: LINQ

My First LINQ Query

Finally, I have had a chance to play around with LINQ. And for the first day of this decade, behold my first LINQ query (executed on the Adventureworks2008 database):

private void Form1_Load(object sender, EventArgs e)
        {
            AdventureWorks2008DataSet1 advWks = new AdventureWorks2008DataSet1();
            AdventureWorks2008DataSet1TableAdapters.BillOfMaterialsTableAdapter bt = new WindowsFormsApplication1.AdventureWorks2008DataSet1TableAdapters.BillOfMaterialsTableAdapter();
            AdventureWorks2008DataSet1TableAdapters.ProductTableAdapter pt = new WindowsFormsApplication1.AdventureWorks2008DataSet1TableAdapters.ProductTableAdapter();
            AdventureWorks2008DataSet1TableAdapters.UnitMeasureTableAdapter ut = new WindowsFormsApplication1.AdventureWorks2008DataSet1TableAdapters.UnitMeasureTableAdapter();
            bt.Fill(advWks.BillOfMaterials);
            pt.Fill(advWks.Product);
            ut.Fill(advWks.UnitMeasure);

            var query =
                from prod in advWks.Product.AsEnumerable()
                join bill in advWks.BillOfMaterials.AsEnumerable()
                on prod.ProductID equals bill.ComponentID
                where prod.FinishedGoodsFlag == true
                && prod.ReorderPoint < 100
                && prod.StandardCost < 800
                select new
                {
                    ProductName = prod.Name,
                    Colour = prod.Color,
                    SafetyLevel = prod.SafetyStockLevel,
                    UnitMeasure = bill.UnitMeasureCode
                };

            dataGridView1.DataSource = query.ToList();

        }