Solved

recursive query in linq to sql

Posted on 2008-10-10
2
1,207 Views
Last Modified: 2013-11-11
I have a database table that lists product categories and sub categories as follows


Products table.............

                  *producttable*
                   id
                   productname
                   categoryid

then just have a productcategory table like this.....

categoryID     categoryParentID     categoryName
1                              0                              first
2                              0                              second
3                              1                             sub category1 from first
4                              1                             sub category2 from first
5                              2                             sub category from second


I want to query these two tables using linq.  
0
Comment
Question by:mugsey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22692610
Hi mugsey;

If you have a Microsoft SQL server or SQL Express you can do the following:
Create a data context class by adding a new Item to the project and the type of the item to create would be a Linq To SQL Classes from the Add New Item dialog box. One you create the data context class drag the two tables on to the design surface and save it. Then you can create the data context class in your code and write the query. If you have a reference constrants in the database this will show up as a line between the two tables in the data context designer. Then you can write your query and access the child table through the parent.

If you do not have a MS SQL database then you will need to create a connection with ADO.Net and load a data set with the tables and query the data table with Linq.

If you would give what the Linq query is to return I will be able to help in constructing the query.

Fernando
0
 

Author Comment

by:mugsey
ID: 22694230
HI Fernando

Thanks for your reply - I will try your suggestion when I get home tonight.  

Thanks
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question