How to make group by in LINQ?

Posted by anwarbesa-15403 under C# on 5/11/2012 | Points: 10 | Views : 6672 | Status : [Member] | Replies : 4
Hi everybody.. I know recently the LINQ in a number of lectures.. it is so interesting topic!

Now I need a c#.net program.. windows application form.. there are three buttons; one to show all information about students(Name, Level, and Marks) on a listview or what I should put? list box or datagridview?
another button for group by; to show the information of students grouped by any thing(Name,Level, or Marks).. and the last button to show the successed students..

so what I should write in the code of these three buttons? plz help me?

C is a sea.. and you are the sea of C!

Thanks & Regards



Responses

Posted by: Sheonarayan on: 5/11/2012 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Dear Anwar,

Read this article that contains frequently used LINQ methods explanations that also describe group by method.

http://www.dotnetfunda.com/articles/article993-frequently-used-linq-extension-methods.aspx

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

anwarbesa-15403, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: anwarbesa-15403 on: 5/11/2012 [Member] Starter | Points: 25

Up
0
Down
Dear Sheonarayan

Thanks for ur replying.. I was seen the article.. it is so helpful

I do this code for the first two buttons for show all and grouped by:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinqAssignment
{
public partial class Form1 : Form
{
class Student
{
public string Name { get; set; }
public int Level { get; set; }
public double Marks { get; set; }
}
List<Student> list = new List<Student>();
public Form1()
{
InitializeComponent();
list.Add(new Student() { Name = "Ahmad", Level = 2, Marks = 90 });
list.Add(new Student() { Name = "Ahmad", Level = 1, Marks = 80 });
list.Add(new Student() { Name = "Khaled", Level = 1, Marks = 95 });
list.Add(new Student() { Name = "Amjad", Level = 3, Marks = 40 });
}

private void button1_Click(object sender, EventArgs e)
{
var result = from d in list select d;
dataGridView1.DataSource = result.ToList();
}

private void button2_Click(object sender, EventArgs e)
{
var result = from d in list group d by d.Name;
dataGridView1.DataSource = result.ToList();
}
}
}


But I do n't know how to make the last button to show only the successed students..

Anyone can help me.. should I put this condition:

var result=from d in list select d.Marks>=50;


Does it will work ?

Thanks and regards






















C is a sea.. and you are the sea of C!

Thanks & Regards

anwarbesa-15403, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: anwarbesa-15403 on: 5/11/2012 [Member] Starter | Points: 25

Up
0
Down
Hay I do it!.. wooooooooow nice!

on button three write this code:

var result = from d in list where d.Marks >= 50 select d;
dataGridView1.DataSource = result.ToList();


Thanks and regards

C is a sea.. and you are the sea of C!

Thanks & Regards

anwarbesa-15403, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saclines on: 8/2/2013 [Member] Starter | Points: 25

Up
0
Down
Please refer the following links for more examples of LINQ GroupBY
http://www.dotnetlines.com/Blogs/tabid/85/EntryId/40/Find-duplicate-rows-in-datatable-using-LINQ.aspx
It finds the duplicates rows in a datatable

http://www.dotnetlines.com/Blogs/tabid/85/EntryId/36/Find-the-occurrences-of-an-array-element-using-LINQ-GroupBy.aspx
It will find and give you the duplicate elements in an array and how many times they have appeared in the array

Sachin N. S.

anwarbesa-15403, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response