Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6335179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:48:21+00:00 2026-05-24T18:48:21+00:00

I’m using this code from the another web: How can I model this class

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:48:22+00:00Added an answer on May 24, 2026 at 6:48 pm

    Edit:

    You’re probably best to pass in an instance of the NorthwindDataContext versus creating a new one with each pass.

    You could do this by creating a method as below, which has been refactored so that it doesn’t need to be recursive which should help a little in the readability department.

        public Objective GetNode(IEnumerable<Objective> collection, params int[] indices)
        {
            Objective current = null;
    
            for (int t = 0; t < indices.Length; t++)
            {
                Objective item = collection.SingleOrDefault(x => x.Parent == current && x.Rank == indices[t] - 1);
    
                if (item == null)
                    return null;
            }
    
            return current;
        }
    

    To be called like: GetNode(cd.Objective, LevelTextBox.Text.ToIntArray());


    Original:
    You could use something like this, it’s just a simple Extension method:

        public static TreeViewItem Get(this TreeView tree, params int[] indexes)
        {
            if (tree == null)
                return null;
    
            if (indexes == null || indexes.Length == 0)
                return null;
    
            TreeViewItem i = tree.Items[indexes[0] - 1] as TreeViewItem;
    
            for (int index = 1; index < indexes.Length; index++)
            {
                i = i.Items.Count >= indexes[index] - 1 ? i.Items[indexes[index] - 1] as TreeViewItem : null;
    
                if (i == null)
                    return null;
            }
    
            return i;
        }
    

    And would be used by treeView1.Get(1,3,1); or in the case of your edit, treeView1.Get(LevelTextBox.Text.Split('.').Select(x => int.Parse(x)).ToArray()); however, this has zero error handling for invalid input.

    If you can’t be certain that all the items will be TreeViewItem objects, you can replace the tree.Items[...] with tree.ItemContainerGenerator.ContainerFromIndex(...) (and same with i.Items

    These changes will require the TreeView to have been rendered fully, however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.