Check column exists in SqlReader using Linq

The only way to check if a column exists in a SqlDataReader is by looping through the columns

http://stackoverflow.com/questions/373230/check-for-column-name-in-a-sqldatareader-object

can anyone give me some linq to do this so I can put the check in a single statement?
purplesoupProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Navneet.Net Full Stack DeveloperCommented:
HI!

You can try this as well

if (reader.GetSchemaTable().Columns["COLUMNNAME"] != null)


Thanks!

purplesoupProgrammerAuthor Commented:
This isn't quite right is it - the SchemaTable returned is a table about the resultset, and the columns are ColumnName, ColumnOrdinal, ColumnSize etc, so it will only tell me about the columns in the schema table, not the columns in the resultset.

If for example I want to know if a column called "Sequence" is in my result set,

if (reader.GetSchemaTable().Columns["Sequence"] != null)

won't do it.
Navneet.Net Full Stack DeveloperCommented:
Hi!

add this class
    static  class MyName
    {

        /// <summary>
        /// Checks if a column exists in the DataReader
        /// </summary>
        /// <param name="dr">DataReader</param>
        /// <param name="ColumnName">Name of the column to find</param>
        /// <returns>Returns true if the column exists in the DataReader, else returns false</returns>
        public static Boolean ColumnExists(this IDataReader dr, String ColumnName)
        {
            for (Int32 i = 0; i < dr.FieldCount; i++)
                if (dr.GetName(i).Equals(ColumnName, StringComparison.OrdinalIgnoreCase))
                    return true;

            return false;
        }
    }

Open in new window


Adding above extension to IDataReader
check your sqlDataReader for Column like :

sqlDataReader.ColumnExists("Sequence");

Thanks!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
purplesoupProgrammerAuthor Commented:
That's pretty clever - I'll wait a little longer if you don't mind just to see if anyone comes up with anything as good...
purplesoupProgrammerAuthor Commented:
Sorry - I didn't actually intend to wait quite so long..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.