简体   繁体   中英

How do you get the index of a column by the column name in an IList?

I apologize if this happens to be an easy question, but I'm new to coding. I've been banging my head against a wall with this problem for several days now on and off and have done numerous internet searches, but I keep coming up empty handed.

I have captured data from a database view into an IList. Because the data comes from a view and might (potentially) change the order of how columns appear should changes ever need to be made (columns added, etc), I want to programmatically pull the column's index from the IList via the column name.

This is easy enough to do with a gridview since it has a nice "HeaderRow" property that allows me to write a bit of code to loop through any row and pull the headerRow name, compare it to the name I'm looking for and then pull an incremented count number when I find it, and Voila!, I then have the index.

The problem is, I'm trying to avoid having to copy data from a list that was already copied from the entity framework that was copied from the database itself into another container (gridview) in order to have to manipulate it. It just seems like it's getting needlessly complex involving a gridview.

So, if anyone can tell me if there's a useful property of the IList class that I can use to acquire the column index # via the column name, please let me know and many thanks!

Edit:

In response to nvoigt's comment "An IList does not know anything about columns."

I'm sure it must have some idea, because when I step through the code and look at the first row data in the IList, it has the column names there ("AppAnswer", etc). All I want is some way to look through them and determine (by property or by code) what index that column name sits at. See photo for clarification.
I've had to link it here since my rep isn't high enough to post photos: https://www.dropbox.com/s/2xg3nduhqnzbl8n/ILisst.JPG

Also, this is the code that captures the data from the view and puts it into the list:

IList<View_AppQnA_All> list1 = useful.GetQuestionAnswerListForJobPosition(1);

public IList<View_AppQnA_All> GetQuestionAnswerListForJobPosition(int jobID)
{
    IList<View_AppQnA_All> QuestionAnswerList = new List<View_AppQnA_All>();

    IQueryable<View_AppQnA_All> view = Repository.Current.ObjectContext.
                View_AppQnA_All.Where(Q => Q.AppQuestionForJobPosition == jobID);
    QuestionAnswerList = view.ToList<View_AppQnA_All>();

    return QuestionAnswerList;
}

#region Imports
using System;
using System.Drawing;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
#endregion

namespace AOJobApplication
{
    class Repository
    {
        #region Declarations

        private static Repository m_instance;
        private static AOApplicationContext CWEntities;
        // Lock synchronization object
        //private static object syncLock = new object();
        private static readonly object syncLock = new object();

        #endregion

        #region Properties

        /// <summary>
        /// Returns the one and only Repository
        /// </summary>
        public static Repository Current
        {
            get
            {
                if (m_instance == null)
                {
                    lock (syncLock)
                    {
                        if (m_instance == null)
                        {
                            m_instance = new Repository();
                        }
                    }
                }

                return m_instance;
            }
        }

        #endregion

        /// <summary>
        /// Object context to get entity objects application void
        /// </summary>
        public AOApplicationContext ObjectContext
        {
            get
            {
                if (CWEntities != null)
                {
                    return CWEntities;
                }
                else
                {
                    CWEntities = new AOApplicationContext();

                    return CWEntities;
                }
            }
        }
    }
}

The object you store in your list has properties, modeled after your view. Properties in a class don't have any order. They are just properties, they exist next to each other. You can order them in any way you like, the Visual Studio debugger seems to order them by name.

Those classes are generated, not dynamically built on the fly. If your view changes drastically (I guess by more than just field order), you will have to generate them again. This means recompile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM