0

I just started to use linq to select data and populate treeview with the data. Please let me know from the basic level...

This is what I did so far.

  1. I connected to DB server. Drag from Tables list, so I can see the table that I need to use from right place. (DataClasses1.dbml)
  2. How do I select data from the table by using linq?

I was trying to imitate this question

But I get the following error: "could not find the implementation of the query pattern for the source type"

private void Form1_Load(object sender, EventArgs e)
{          
            var grped = 
                 from a in MyTable
                 group a by a.MyColumn into grp
                 select grp;
            var treeView = new System.Windows.Forms.TreeView();

            //
}
Community
  • 1
  • 1
Elena
  • 3
  • 1
  • 3

1 Answers1

0

Assuming you have populated DB data into MyTable (It should be a Collection that supports LINQ for example an IEnumerable) and want to group them by MyColumn:

Variable grped has the groups of MyTable data grouped by MyColumn.

Then iterate through the grped groupings and populate TreeView with each groups KEY (That is DISTINCT values of MyColumn data) as parent node and each grouped sub elements as the parent node's children.

I suspect MyTable is not in a form that can be used with LINQ. Also check you have the following directive using System.Linq;

Nicky
  • 75
  • 7
  • Is there anything to populate DB data into MyTable? I just connected to database and MyTable is just one of the table name. above my coding is not working. Error at 'from a in Mytable' – Elena Sep 02 '14 at 09:21
  • Follow these links: http://msdn.microsoft.com/en-us/library/jj943772.aspx AND http://msdn.microsoft.com/en-us/library/h0y4a0f6.aspx – Nicky Sep 02 '14 at 09:28
  • How did you connect to database? – Nicky Sep 02 '14 at 09:28
  • at 'Server Explorer' of Visual Studio. So now I have DataClasses1.dbml in my Solution Explorer – Elena Sep 02 '14 at 09:38
  • I assume you are trying LINQ to SQL. You should get DataContext object to get the data into your application. Follow this link to get DataContext. http://msdn.microsoft.com/en-us/library/bb384470.aspx – Nicky Sep 02 '14 at 09:48
  • Did you find the links or the answer useful? – Nicky Sep 03 '14 at 04:56