i have a simple query in Linq which gets the characters group by word length, below is the code
string sentence = "This is sample linq query";
string[] words = sentence.Split(' ');
var query = from word in words
group word by word.Length into gr
orderby gr.Key ascending
select new { Length = gr.Key, Words = gr };
foreach (var obj in query)
{
Console.WriteLine("Word of Length: {0}", obj.Length);
foreach (string word in obj.Words)
{
Console.WriteLine("{0}", word);
}
Console.WriteLine();
}
it works properly, now i want to convert this into the windows form application by placing the above records into DataGridView, so i implemented like below.
string sentence = "This is sample linq query";
string[] words = sentence.Split(' ');
var query = from word in words
group word by word.Length into gr
orderby gr.Key ascending
select new { Length = gr.Key, Words = gr };
dataGridView1.DataSource = query.ToList();
but here i just gets first coluumn (Length) in the DataGridView and not Words column, is there anything else i need to do to get both columns.
In your case the words-part of the grouping consists of an IGrouping – which cannot be displayed by the datagrid. Try concatenating the words as follows:
Then you will see two columns in the grid, one with the length, the other with the now again concatenated words of that length.