I'm using this code from the another web:
How can I model this class in a database?
I have in each objective record a field named "Rank". It tells me what position is. For instance:
Objective "Geometry": Rank1
|_Objective "Squares": Rank1
|_Objective "Circles": Rank2
|_Objective "Triangle": Rank3
|_Objective "Types": Rank1
Objective "Algebra": Rank2
Objective "Trigonometry": Rank3
That rank tells me the order of the nodes. But I want to get all the rank:
Objective "Geometry": Rank1
|_Objective "Squares": Rank1 -> 1.1
|_Objective "Circles": Rank2
|_Objective "Triangle": Rank3
|_Objective "Types": Rank1 -> 1.3.1
Objective "Algebra": Rank2
Objective "Trigonometry": Rank3 -> 3
I'm using LINQ to SQL.
<TreeView Name="treeView1">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:Objective}" ItemsSource="{Binding Path=Objectives}" >
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
I need a linq function where I can get a specified node. I mean, a function which gets the node through the level (1.2), (1.3.1)
If exists, return the node, if not null.
UPDATE 1:
This is not really a function, but I realized it's better to create a getNode function.
private void AddButton_Click(object sender, RoutedEventArgs e)
{
NorthwindDataContext cd = new NorthwindDataContext();
int[] levels = LevelTextBox.Text.ToIntArray('.');
string newGroupName = NameTextBox.Text;
Objective currentObjective = null;
int? identity = null;
for (int i = 0; i < levels.Length - 1; i++ )
{
int currentRank = levels[i];
if (identity == null)
{
currentObjective = (from p in cd.Objective
where p.Level == currentRank && p.Parent_ObjectiveID == null
select p).SingleOrDefault();
}
else
{
currentObjective = (from p in cd.Objective
where p.Level == currentRank && p.Parent_ObjectiveID == identity
select p).SingleOrDefault();
}
if (currentObjective == null)
{
MessageBox.Show("Levels don't exist");
return;
}
else
{
identity = currentObjective.ObjectiveID;
}
}
if (currentObjective != null)
{
if (levels.Last() == currentObjective.Level)
{
MessageBox.Show("Level already exists");
return;
}
}
else
{
var aux = (from p in cd.Objective
where p.Parent_ObjectiveID == null && p.Level == levels.Last()
select p).SingleOrDefault();
if (aux != null)
{
MessageBox.Show("Level already exists");
return;
}
}
var newObjective = new Objective();
newObjective.Name = NameTextBox.Text;
newObjective.Level = levels.Last();
newObjective.Parent_ObjectiveID = currentObjective == null ? null : (int?)currentObjective.ObjectiveID ;
cd.Objective.InsertOnSubmit(newObjective);
cd.SubmitChanges();
}
UPDATE 2:
public Objective GetNode(params int[] indexes)
{
return GetNode(null, 0, indexes);
}
public Objective GetNode(int? parentid, int level, params int[] indexes)
{
NorthwindDataContext cd = new NorthwindDataContext();
Objective item = null;
if (indexes.Length == 0)
return null;
if (parentid == null)
{
item = (from p in cd.Objective
where p.Level == indexes[level] && p.Parent_ObjectiveID == null
select p).SingleOrDefault();
}
else
{
item = (from p in cd.Objective
where p.Level == indexes[level] && p.Parent_ObjectiveID == parentid
select p).SingleOrDefault();
}
if (item == null)
return null;
if (++level < indexes.Length)
item = GetNode(item.ObjectiveID, level, indexes);
return item;
}