LINQ C# Creating Dictionary of Dictionaries using GroupBy

I can't seem to find a solution to this, and the error message is not very helpful.

I have a DataTable:

iGuid, iDateTime, iDecimal

I want to create a Dictionary of Dictionaries, such that...

iGuid -> iDateTime -> iDecimal.

For example.

Guid1, Monday, 4 Guid1, Tuesday, 4 Guid1, Friday, 1 Guid2, Monday, 1 Guid2, Tuesday, 1

Now I want this to be made into a dictionary with the key of iGuid, and the value another Dictionary with the key of iDateTime, and the value of iDecimal.

My code right now is:

Dictionary<Guid, Dictionary<DateTime, decimal>> val = iDataTable.OfType<CustomDataRow>().GroupBy(r => new { r.iGuid } ) .ToDictionary<Guid, Dictionary<DateTime, decimal>>( row => row.Key.iGuid, child => child.ToDictionary<DateTime, decimal>(subKey => subKey.iDateTime, subVal => subVal.iDecimal));

I have to return this typed, as I need to use it in other functions (If I throw a var in front of it, it's fine).

-------------Problems Reply------------

The problem with your implementation is the wrong usage of the method:

public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector

As you can see the types specified in this call is not the expected return type of the dictionary.

Since you are not using the source (CustomDataRow) as the value Type of your dictionary, you cannot use this signature.

This one should do the job:

Dictionary<Guid, Dictionary<DateTime, decimal>> val =
.GroupBy(r => r.iGuid)
.ToDictionary(group => group.Key,
group => group.ToDictionary(dataRow => dataRow.iDateTime,
dataRow => dataRow.iDecimal));

Category:c# Views:14 Time:2018-04-30

Related post

  • Create Dictionary For Mobile Using J2ME 2009-08-31

    I want to create the dictionary for mobile. But I have any idea about it. Could anybody guide to learn about these? --------------Solutions------------- You need to elaborate a bit. What kind of a dictionary? If what you want a list of word pairs, fo

  • Building expression tree to dynamically sort a dictionary of dictionaries in LINQ 2010-09-30

    I am attempting to dynamically build up an expression tree so that I can change the sort order for data contained in a dictionary of dictionaries. There is lots of information about dynamically specifying the column to sort by, but this is not really

  • Can't use Django's get_absolute_url in dictionary of dictionaries? 2009-08-23

    I'm having some trouble using get_absolute_url in a template. It seems to work fine if I just pass in one of my store objects and say {{ store.get_absolute_url }}, but if I have to iterate through a dictionary of stores and then use the get_absolute_

  • Problem with grid when trying to use groupby in LINQ 2011-03-27

    When trying to use GROUPBY I get an error saying a field 'date1' is not found on selected resource. var query = (from a in db.Dates from b in db.Facts where a.Count_Key == b.Date_key select new{ a.Date1, a.Month, b.Fact_key }); var query2 = query.Gro

  • I'm creating a page that uses Linqfor data access and I'm using DataList to display data. How can I use Linq to do data paging in using C#? 2011-08-24

    I'm creating a page that uses Linqfor data access and I'm using DataList to display data.How can I use Linq to do data paging in anyone plz show both the aspx part aspx.cs part?can anyone plz show both the aspx part aspx.cs part? --------

  • Python - create dictionary from list of dictionaries 2012-02-28

    I have a list of dictionaries in Python [ {'id':'1', 'name': 'test 1', 'slug': 'test1'}, {'id':'2', 'name': 'test 2', 'slug': 'test2'}, {'id':'3', 'name': 'test 3', 'slug': 'test3'}, {'id':'4', 'name': 'test 4', 'slug': 'test4'}, {'id':'5', 'name': '

  • How to use a dictionary of dictionaries as output from Python? 2012-04-03

    This function is driving me insane! def CCAD1 (tree) leaves = [] for otu in tree: if tree[otu][2]== None and tree[otu][1]== None: leaves += [otu] ccad = {} for leaf in leaves: otuX = leaf otu1 = leaf for leaf2 in leaves: otuY = leaf2 otu2 = leaf2 whi

  • Selecting columns with linq with nested dictionary 2010-03-28

    How do I get all of the "columns" using linq from this dictionary. Dictionary<int, Dictionary<int, string>> _cells; where I can access a row this way var someRow = _cells[2]; and I am able to get a cell string cell = _cells[2][2]; What I

  • NHibernate 3.0 Linq query against element instead of using HQL 2010-11-17

    I have a mapping that looks like this: <class name="Record"> <map name="Values"> <key column="RecordFK"/> <index column="FieldFK"/> <element column="Value"/> </map> </class> Translating this to English: a Rec

  • How to use groupBy when you have 2 different groupings? 2012-01-20

    I have this sample data 1/14/2012,5,Gas 1/15/2012,5,Gas 1/16/2012,5,Gas 1/17/2012,5,Gas 1/18/2012,5,Gas 1/19/2012,5,Gas 1/20/2012,5,Gas 1/21/2012,5,Gas 1/22/2012,5,Gas 1/23/2012,5,Gas 1/24/2012,5,Gas 1/25/2012,5,Gas 1/26/2012,5,Gas 1/27/2012,5,Gas 1/

  • How can I create a PDF form using PDFMARK and/or PERL cpan module PDF::API2? 2012-02-25

    Does anyone have any sample that demonstrates how one may create a simple PDF form (single text input box, or single radio button) and 'submit field_name/value pairs via HTTP post' to server when clicked, using either of three methods? By embedding A

  • Linq To SQL OrderBy, issue when using enums 2008-12-18

    I am having some issues with using the OrderBy extension method on a LINQ query when it is operating on an enum type. I have created a regular DataContext using visual studio by simply dragging and dropping everything onto the designer. I have then c

  • Extension method for Dictionary of Dictionaries 2009-05-26

    I'm trying to write an extension method to insert data into a dictionary of dictionaries defined as follows: items=Dictionary<long,Dictionary<int,SomeType>>() What I have so far is: public static void LeafDictionaryAdd<TKEY1,TKEY2,TVAL

  • Linq to SQL: How to delete using only the primary key? 2010-03-08

    How do I delete a record using Linq to SQL using only the primary key, without having to retrieve the exsting record from the database? --------------Solutions------------- You should be able to create an instance of the object with the appropriate F

  • How to Delete Entries In Static Dictionary When Not Used 2010-07-03

    I have a static class that holds a dictionary of objects. I have multiple threads that access this dictionary and create objects on it that are specific to that thread. How and when can I remove entries from the dictionary when a thread is "done"? Ba

  • Query a Dictionary of Dictionaries? 2010-09-16

    Please can you advise me on how to query a Dictionary of Dictionaries, and/or a Dictionary of List? private Dictionary<string, Dictionary<DateTime, double>> masterDict= new Dictionary<string, Dictionary<DateTime, double>>(); P

  • LINQ to create new records in a XML file 2011-06-27

    Question I have a simiar XML file stored in my physical drive , how i can create a new record using C# in this xml file ? , is it possible to do it with LINQ? or is there an any other method ? [other method should not be read the whole file and conca

  • Testing : How to create fake object context using TypeMock for EF4 model 2011-08-17

    I am using EF4 in my application and I want to make test cases for DAL methods, which generally hit the database to get data. I am using the Typemock framework for Mocking. I want to mock database call and only want to test queries. E.g.: ObjectConte

  • How to create dynamic incrementing variable using "for" loop in C# 2011-09-12

    How to create dynamic incrementing variable using "for" loop in C#? like this: track_1, track_2, track_3, track_4. so on. --------------Solutions------------- You can't create dynamically-named variables. All you can do - it to create some collection

  • Complex database structure to Create and Edit views using viewModel in MVC 3 with EF4 2012-01-07

    I had bad times trying to figure out how to deal with this. Any help wold be appreciated. Even a suggestion of a better structure that fits to my needs: Construct category items in a category having an specification of how they can be in a category i

  • create dictionary from list of variables 2012-02-29

    I'm looking for a way to create dictionary without writing the key explicitly I want to create function that gets number variables, and create dictionary where the variables name are the keys and their value is the variable value Instead of writing t

  • How to create Dictionary that can hold anything in Key? or all the possible type it capable to hold 2014-06-09

    I want to create a Dictionary that does not limit the key type (like NSDictionary) So I tried var dict = Dictionary<Any, Int>() and var dict = Dictionary<AnyObject, Int>() resulting error: type 'Any' does not conform to protocol 'Hashable

  • Accessing elements in dictionary of dictionaries 2014-09-19

    I am parsing some information from a CSV File and am inputting it into a dict of dicts. The inner dict v contains the following elements {'140725AD4': <mod.City object at 1x3259C2D1>, '631315AD2': <mod.City object at 0x023A4870>}. How wou

  • How to create a COM dll using Visual Studio 2008 2009-02-24

    How do I create a COM dll using Visual Studio 2008? What are the custom settings needed for creating the dll? That dll should be used in Microsoft Navision(ERP PACKAGE). --------------Solutions------------- Simply create a class library. In the proje

Copyright (C), All Rights Reserved.

processed in 0.094 (s). 11 q(s)