Avatar of AndyC1000
AndyC1000
 asked on

C# Extend LINQ to Query multiple Dictionaries

Hello,

I'm wondering if its possible to extend the following LINQ Query to Query multiple Dictionaries.  I've tried adding a comma followed by the Dictionary name although syntax errors exist.  

The datastores are stored in a List - List<SortedDictionary<DateTime, SortedDictionary<string, double>> >

Thanks

var query = from d in _dataStore
                        where
                            (d.Key >= tempStart) &&
                            (d.Key <= tempEnd)
                        select d.Value;

            var valueQuery = (from sortedDictionary in query
                              let name = n
                              select (from e in sortedDictionary where e.Key == name select e.Value) into query2
                              select query2.Single<double>()
                             )
                             .OrderBy(value => value);

            double total = valueQuery.Any() && valueQuery.First() != -9999 ? valueQuery.Sum() : -9999;

Open in new window

C#

Avatar of undefined
Last Comment
AndyC1000

8/22/2022 - Mon
kaufmed

Can you describe (in non-LINQ terms) what it is you are trying to get from the query? I'm not entirely sure of what you mean by "query multiple dictionaries" in this context.
AndyC1000

ASKER
I have three dictionaries stored in a list
private List<SortedDictionary<DateTime, SortedDictionary<string, double>>> list;

I wrapped the LINQ query in a foreach loop to execute the query for each dictionary accumulating the result.  The following code works as expected.

Is it possible to update the LINQ query below to query each of the dictionaries without the foreach loop.  I also need to catch the instances (to output to log file) where valueQuery is null, when 'n' isn't present in the dictionary (let name = n).

Thanks

double total = 0d;
            double total2 = 0d;
            foreach (var data in list)
            {
                if(data.Count > 0)
                {
                    var query = from d in data
                                where
                                    (d.Key >= tempStart) &&
                                    (d.Key <= tempEnd)
                                select d.Value;

                    var valueQuery = (from sortedDictionary in query
                                      let name = n
                                      select (from e in sortedDictionary where e.Key == name select e.Value) into query2
                                      select query2.Single<double>()
                                     )
                                     .OrderBy(value => value);

                    total = valueQuery.Any() && valueQuery.First() != -9999 ? valueQuery.Sum() : -9999;

                    if (total == -9999)
                    {
                        total2 = -9999;
                    }
                    else
                    {
                        total2 += total;
                    }
                }  
            }

Open in new window

ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AndyC1000

ASKER
Great solution!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23