private void button4_Click(object sender, EventArgs e) { DafestyEntities context = new DafestyEntities(); //var q = from x in context.Customers select x; //Customer c = context.Customers.Where(x => x.CustomerID == "1111").First(); //label1.Text = c.CustomerName; List <Customer> lst = context.Customers.Where(x => x.MemberRating == "A").ToList(); lst = context.Customers.OrderBy(x => x.CustomerName).ToList(); lst = context.Customers.Where(y => y.MemberRating == "A").OrderBy(x => x.CustomerName).ToList(); lst = context.Customers.Where(y => y.MemberRating == "A" && y.CustomerName.Substring(0, 1) == "B").ToList(); dataGridView1.DataSource = lst; int noofAmem = context.Customers.Count(x => x.MemberRating == "A"); label1.Text = (noofAmem).ToString(); decimal?d = context.Customers.Sum(x => x.AmountSpent); // nullable datatypes label1.Text = (d).ToString(); }
private void button3_Click(object sender, EventArgs e) { DafestyEntities context = new DafestyEntities(); var q = from x in context.Customers where x.MemberRating == "A" && x.CustomerName.Substring(0, 1) == "R" orderby x.CustomerName select x; List <Customer> lst = q.ToList(); dataGridView1.DataSource = lst; }
private void button2_Click(object sender, EventArgs e) { DafestyEntities context = new DafestyEntities(); // write a LINQ Query (don't need to type SQL) var q = from x in context.Customers where x.CustomerID == "1007" select x; // get first object from Collection Customer c = q.First(); label1.Text = c.CustomerName; }
/// <summary> /// Example of LINQ Query returning only selected columns /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click(object sender, EventArgs e) { //DafestyEntities context = new DafestyEntities(); //var q = from x in context.Customers // select new { x.CustomerID, x.CustomerName, x.MemberRating }; // deriving anonymous class from the Customers class //var q1 = from x in context.Customers select x; //Customer c = q1.First(); //dataGridView1.DataSource = q.ToList(); DafestyEntities context = new DafestyEntities(); var q = from x in context.Customers group x by x.MemberRating into g select new { MemberCategory = g.Key, AMT = g.Sum(y => y.AmountSpent) }; dataGridView1.DataSource = q.ToList(); }
/// <summary> /// Example of updating, adding and inserting the DB with EF /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { DafestyEntities ctx = new DafestyEntities(); Customer c = ctx.Customers.First(x => x.CustomerID == "1007"); // Lambda expression label1.Text = c.CustomerName + " " + c.MemberRating; c.MemberRating = "B"; label1.Text = c.CustomerName + " " + c.MemberRating; //// create a new customer //Customer cN = new Customer(); //cN.CustomerID = "1007"; //cN.CustomerName = "Bond"; //cN.MemberRating = "Z"; //// added to the customers collection //ctx.Customers.Add(cN); // remove from customers collection ctx.Customers.Remove(c); ctx.SaveChanges(); // save your changes }
private void button1_Click(object sender, EventArgs e) { DafestyEntities context = new DafestyEntities(); dataGridView1.DataSource = context.Customers.ToList(); }