• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

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

Hi,
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.
0
jackjohnson44
Asked:
jackjohnson44
1 Solution
 
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:

datarow["gridRowId"]

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.
0
 
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.
0
 
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)
    {
        justGridRowsIds.Add(dataRows[i]);
    }
}

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.
0
 
CottonSwabCommented:
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.
0
 
NavneetSoftware EngineerCommented:
Hi!

Try this instead and check speed

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

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now