Solved

Custom selection from dataset

Posted on 2004-08-06
3
262 Views
Last Modified: 2010-04-15
Hi Experts,

I generate a dataset with 3 column: PathId, ParentPathId, Path.

Example:
PathId                            ParentPathId                      Path
1                                   null                                    path1
2                                   1                                       path2
3                                   2                                       path3
4                                   2                                       path4
5                                   null                                    path5
6                                   5                                       path6

What I want to get is those paths:
path1
path1/path2
path1/path2/path3
path1/path2/path4
path5
path5/path6

Thanks in advance to help me to retrieve those paths.
0
Comment
Question by:noulouk
[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
3 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11740722
you are going to need to make a recursive function in order to get that, no compiler in front of me but this should be close.

public string GetPath(int ParentId) {
    DataView dv = new DataView(ds.Tables["whatever"]);
    dv.RowFilter = "PathId = " + ParentId;
    if(dv.Rows.Count == 1) {
          return dv.Rows[0].Item["Path"].ToString() + "/" + GetPath(dv.Rows[0].Item["PathId"];
    } else if(dv.Rows.Count > 1) {
          throw new System.Exception("More than one child directory found for " + ParentId);
    }
}

then to build all ...

public StringCollection GetAllPaths() {
      StringCollection sc = new StringCollection();
      DataView dv =  new DataView(ds.Tables["whatever"]);
      dv.RowFilter = "ParentPathId = null";
      foreach(DataRowView dr in dv.Rows) {
             sc.Add(GetPath((int) dr["PathId"]));
      }
}
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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