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 8700221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:07:52+00:00 2026-06-13T02:07:52+00:00

I have two methods that almost do the same thing. They get a List<XmlNode>

  • 0

I have two methods that almost do the same thing. They get a List<XmlNode> based on state OR state and schoolType and then return a distinct, ordered IEnumerable<KeyValuePair<string,string>>. I know they can be refactored but I’m struggling to determine what type the parameter should be for the linq statement in the return of the method (the last line of each method).

I thank you for your help in advance.

private IEnumerable<KeyValuePair<string, string>> getAreaDropDownDataSource() {
    StateInfoXmlDocument stateInfoXmlDocument = new StateInfoXmlDocument();
    string schoolTypeXmlPath = string.Format(STATE_AND_SCHOOL_TYPE_XML_PATH, StateOfInterest, ConnectionsLearningSchoolType);
    var schoolNodes = new List<XmlNode>(stateInfoXmlDocument.SelectNodes(schoolTypeXmlPath).Cast<XmlNode>());
    return schoolNodes.Select(x => new KeyValuePair<string, string>(x.Attributes["idLocation"].Value, x.Value)).OrderBy(x => x.Key).Distinct();
}

private IEnumerable<KeyValuePair<string, string>> getStateOfInterestDropDownDataSource() {
    StateInfoXmlDocument stateInfoXmlDocument = new StateInfoXmlDocument();
    string schoolTypeXmlPath = string.Format(SCHOOL_TYPE_XML_PATH, ConnectionsLearningSchoolType);
    var schoolNodes = new List<XmlNode>(stateInfoXmlDocument.SelectNodes(schoolTypeXmlPath).Cast<XmlNode>());
    return schoolNodes.Select(x => new KeyValuePair<string, string>(x.Attributes["stateCode"].Value, x.Attributes["stateName"].Value)).OrderBy(x => x.Key).Distinct();
}
  • 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-06-13T02:07:53+00:00Added an answer on June 13, 2026 at 2:07 am

    Extract nodes retrieving to separate methods/properties. I also suggest to have different properties/methods for extracting school and state nodes:

    private List<XmlNode> GetNodes(string xPath)
    {
        XmlDocument stateInfoXmlDocument = new XmlDocument();
        return new List<XmlNode>(stateInfoXmlDocument.SelectNodes(xPath)
                                                     .Cast<XmlNode>());
    }
    
    private List<XmlNode> SchoolNodes
    {
        get { return GetNodes(String.Format(SCHOOL_PATH, LearningSchoolType)); }
    }
    
    private List<XmlNode> StateNodes
    {
        get { return GetNodes(String.Format(STATE_PATH, StateOfInterest)); }
    }   
    

    Use union of school and state nodes for retrieving area nodes:

    private IEnumerable<KeyValuePair<string, string>> GetAreaDropDownDataSource()
    {
        return SchoolNodes.Union(StateNodes)
                .Select(x => new KeyValuePair<string, string>(x.Attributes["idLocation"].Value, x.Value))
                .OrderBy(x => x.Key)
                .Distinct();
    }
    
    private IEnumerable<KeyValuePair<string, string>> GetStateOfInterestDropDownDataSource()
    {
        return SchoolNodes
            .Select(x => new KeyValuePair<string, string>(x.Attributes["stateCode"].Value, x.Attributes["stateName"].Value))
            .OrderBy(x => x.Key)
            .Distinct();
    }
    

    Also you can use different selectors of type Func<XmlNode, KeyValuePair<string, string>> and pass them to method which will create data source:

    private IEnumerable<KeyValuePair<string, string>> GetDropDownDataSource(
            List<XmlNode> nodes,
            Func<XmlNode, KeyValuePair<string, string>> selector)
    {
        return nodes.Select(selector)
                    .OrderBy(x => x.Key)
                    .Distinct();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods that look almost the same, except for the aggregate function
I'm having trouble grasping generic methods. I have two classes that are generated (they
I have two action methods that are conflicting. Basically, I want to be able
I have the following two methods that (as you can see) are similar in
I have two methods, one that I use to convert an image to a
Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
I have these two methods on a class that differ only in one method
Suppose you have two static libraries A and B such that A references methods
Imagine two classes which share almost the same exact methods and properties, both extending
I have two classes that are almost identical, besides one method. The classes have

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.