多级分层数据排序C#

时间:2017-10-16 10:03:03

标签: c# linq sorting

我有一个实体:

public class Document
{
    public int id;
    public int? ParentId;
    public string name;
}

数据库中的数据:

id  |  ParentId | name
-----------------------
1   |           |  fruits
2   |           |  vegetables
3   |     2     |  tomatoes
4   |     1     |  apples 
5   |     4     |  golden apples
6   |     4     |  red apples

或者,如C#:

var documents = new[]
{
    new Document() { id = 1, ParentId = null, name = "fruits" },
    new Document() { id = 2, ParentId = null, name = "vegetables" },
    new Document() { id = 3, ParentId = 2, name = "tomatoes" },
    new Document() { id = 4, ParentId = 1, name = "apples" },
    new Document() { id = 5, ParentId = 4, name = "golden apples" },
    new Document() { id = 6, ParentId = 4, name = "red apples" },
};

我需要得到:

id  |  ParentId | name
-----------------------
1   |           |  fruits
4   |     1     |  apples
5   |     4     |  golden apples
6   |     4     |  red apples
2   |           |  vegetables
3   |     2     |  tomatoes

如何在c#中快速排序分层数据?

1 个答案:

答案 0 :(得分:3)

所以,给定:

UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];

self.monthPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];

    self.monthPickerView.backgroundColor = [UIColor whiteColor];

    self.monthPickerView.delegate = self;

    self.monthPickerView.dataSource = self;
[inputView addSubview:self.monthPickerView];

    cell.monthTextField.inputView = inputView ;

self.monthTextField.inputAccessoryView = [self doneButtonAccessoryView];


// doneButtonAccessoryView Method


-(UIToolbar*)doneButtonAccessoryView
{

    UIToolbar *kbToolbar = [[UIToolbar alloc] init];
    [kbToolbar sizeToFit];
    [kbToolbar setBarTintColor:[UIColor whiteColor]];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                   style:UIBarButtonItemStyleDone target:self
                                                                  action:@selector(doneClicked)];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                     style:UIBarButtonItemStyleDone target:self
                                                                    action:@selector(cancelClicked)];
    NSDictionary *attrDict;

    attrDict = [NSDictionary dictionaryWithObjectsAndKeys:
                [UIFont fontWithName:@"Helvetica-Bold" size:16.0], NSFontAttributeName, nil];
 [doneButton setTitleTextAttributes:attrDict forState:UIControlStateNormal];
    UIBarButtonItem *flexWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                               target:self action:nil];

    [kbToolbar setItems:[NSArray arrayWithObjects:cancelButton,flexWidth, doneButton, nil]];
    return kbToolbar;
}

betareg(value ~ category | 1, data = df, subset = category != "c1")
## Call:
## betareg(formula = value ~ category | 1, data = df, subset = category != 
##     "c1")
## 
## Coefficients (mean model with logit link):
## (Intercept)   categoryc3   categoryc4   categoryc5  
##      0.2634      -2.2758      -4.4627      -1.0206  
## 
## Phi coefficients (precision model with log link):
## (Intercept)  
##       2.312  
betareg(value ~ category | category, data = df, subset = category != "c1")
## Call:
## betareg(formula = value ~ category | category, data = df, subset = category != 
##     "c1")
## 
## Coefficients (mean model with logit link):
## (Intercept)   categoryc3   categoryc4   categoryc5  
##      0.2566      -2.6676      -4.0601      -0.9784  
## 
## Phi coefficients (precision model with log link):
## (Intercept)   categoryc3   categoryc4   categoryc5  
##      2.0849       3.5619      -0.2308      -0.1376  

然后这个有效:

public class Document
{
    public int id;
    public int? ParentId;
    public string name;
}

它给你:

Sorted Documents