c# is linq the best way to extract one element out of a dictionary?

I am have a list of dictionary objects and I want a list of strings.  The list of strings will be one key from the dictionary.
            var dataRows = new List<Dictionary<string, string>>();
            dataRows = getRows()// passes back a
            //after that, get a list with just gridRowId
            var justGridRowIds =
                from datarow in dataRows
                where datarow["gridRowId"] != ""
                select datarow["gridRowId"];

This seems very slow.  Is there a better way?  The datarows list could have tens of thousands of elements.
Who is Participating?

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

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.

käµfm³d 👽Commented:
It's a Dictionary (a.k.a. HashTable)...  the simplest way to retrieve elements is to pass it the key you are interested in:

Dictionary<string, string> myDictionary = new Dictionary<string, string>()
    { "one", "red" },
    { "two", "blue" },
    { "three", "green" },

string itemTwo = myDictionary["two"];

Open in new window

...but this is exactly what you are doing. You are actually using Linq to enumerate the List object. Within the Linq query, you are accessing the Dictionary as I outlined above:


In your query, datarow is a Dictionary<string, string>. Why? Because your query iterates over the List using datarow as a temporary iterator variable. Each item in the List is a Dictionary<string, string>.

You might see better performance from a regular for or foreach loop over the List.
jackjohnson44Author Commented:
Thanks, I understand what is going on.
My questions is, what is the optimal way to do what my code above is doing?
I have one way that works.  I am looking for the fastest.  I am not insisting on linq.  That was my first attempt.
käµfm³d 👽Commented:
My guess is a for loop:

var justGridRowIds = List<Dictionary<string, string>>();

for (int i = 0; i < dataRows.Count; i++)
    if (dataRows[i]["gridRowId"] != string.Empty)

Open in new window

You'll need to run timing tests on your system to confirm, but it should be faster than a Linq approach.
First off, I would question what getRows() is doing... based on the usage it looks like it is probably querying a database.  If this is the case, you probably should either refactor getRows or add other method that fires off a query based on the gridRowId.  This will speed things up considerably.
Navneet.Net Full Stack DeveloperCommented:

Try this instead and check speed

var justGridRowIds = dataRows.FirstOrDefault(x => x.Name=="jack");


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
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

From novice to tech pro — start learning today.