Flatten A Dictionary Of Dictionaries And Sum The Values Of The Inner Dictionary With LINQ?
Apr 16, 2012
I have the following object:
countDictionary As Dictionary(of Category, Dictionary(of Date, Integer))
The Class has a Enumeration Property. For the purposes of demonstration, I'll call it MasterCategory.I have been trying to get out an object that looks like the following:
groupedCountDictionary As Dictionary(of MasterCategory, Dictionary(of Date, Integer)
The best result I could get was:
Lookup(of MasterCategory, Dictionary(of Date, Integer))
From:
countDictionary.ToLookup(Function(o) o.Key.MasterCategory, Function(o) o.Value)
Which results in a IEnumerable (Of Dictionary(of Date, Integer)) for each MasterCategory value.However, I need that IEnumerable of Dictionary flattened to one dictionary with all the integers summed (total counts) for each date. I then tried to use various selects and group bys (from numerous stackoverflow posts) to "flatten" it, but my efforts have fallen short.
Current Code
[Category Class]
- MasterCategory As Enum
- Name As String etc
[code]....
View 1 Replies
ADVERTISEMENT
Jan 25, 2010
I want to group items from a linq query under a header, so that for each header I have a list of objects that match the header title. I assumed the solution would be to use ToDictionary to convert the objects, but this allows only one object per "group" (or dictionary key). I assumed I could create the dictionary of type (String, List Of()), but I can't figure out how to write it. As an example I have written a simplified version below.
[Code]...
View 2 Replies
Jul 7, 2010
I have created a Dictionary class (MyDictionary for the example). I am currently trying to pass MyDictionary into a function, filter it into a new instance of MyDictionary and pass this new instance into another method. When I am attempting to create the second instance from the filtered first instance of MyDictionary via Lambda Expressions and the ToDictionary Method, I am getting the following error:
Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.Int32,System.String]' to type 'MyDictionary'. I have simplified the example and recreated it in LINQPad and am getting the same error.
Here's the simplified version of my code:
[Code]...
View 2 Replies
Apr 27, 2009
I have created a class with a function in it. I have a collection of data I want to pass back. I tried an arraylist first. Now I am trying to use a dictionary. My problem is that it creates the dictionary ok, but I am only get the last row of data from my
Function GetWeldAuditInfo(ByVal ResourceId
As
String,
ByVal VendorId
[CODE].........................
View 2 Replies
Jan 6, 2011
I use VS2005 and I have just started working with the dictionary in particular the Dictionary.ContainsKey method. At the bottom of the page in the msdn library it says the following in the community content How to make sure that Contains functions properly.
View 3 Replies
Mar 23, 2012
I'm trying to sort a dictionary by value with LINQ but I can't figure how the ToDictionary() method works.
All the examples I can find are in c#.
here's my code
Dim f As Dictionary(Of String, String) = (From value In projectDescriptions.Values
Order By value Ascending
Select value).ToDictionary(???)
[Code].....
View 3 Replies
Mar 31, 2010
In My Code i am using Dictionary(Of String , String) As Key Value Pair ..While i am using LINQ to Query this Dictionary, It Shows Dictionary is not Queryable.
View 1 Replies
Jan 2, 2010
As I am beginner in LINQ, I have small problems. I have dictionary of Char and Long, and I would like to use LINQ to retrieve Char with highest Long Value. I saw some C# samples, which look pretty easy, but I can't achieve such expression in VB.Here are two expressions, and I don't think they are most efficient possible.
[Code]...
View 14 Replies
May 23, 2012
This should be extremely simple but I'm still new to the language and don't grasp the basics yet.
[Code]...
View 1 Replies
May 23, 2012
I need a simple LINQ query on VB.NET on the dictionary [Code] What I need here is to retrieve a SINGLE website (dictionary value of string type) or NOTHING (if the query cannot find an affordable result) given those rules: The dictionary key (integer) must be greater than startSite and not equal to mainSite or returnSite (both must be excluded from the result) Any hint? [Code]
View 2 Replies
Jun 7, 2010
I have a dictionary collection as bleow:
mydic.addvalue(key1, val1)
mydic.addvalue(key2, val1)
mydic.addvalue(key3, val1)
mydic.addvalue(key4, val2)
mydic.addvalue(key5, val2)
From the above dictionary I want to delete all the entries where value == "val1", so that the result would have only following entry:
mydic.addvalue(key4, val2)
mydic.addvalue(key5, val2)
My VB source code is on VS2008 and targeted for 3.5
View 1 Replies
Mar 14, 2012
I have a Dictinary(Of String, Item) and I'm trying to sort it into alphabetical order by the item name. I don't want to use a sorted dictinary and without it, I've having zero luck. Linq is not my strong point.
[Code]...
View 2 Replies
Nov 23, 2010
How to avoid for loop (Dictionary Array) using LINQ
View 1 Replies
Dec 11, 2009
I've got a Dictionary(Of SomeEnum, Integer) that gets filled up while looping through some objects that have a property with type SomeEnum. Once the loop is done, I want the SomeEnum type that occurs the most in the list of objects. I also need the other counts as well for display purposes, hence the usage of a simple Dictionary(Of K, V).I am looking for a LINQ query to give me back the SomeEnum key that occurs the most by looking at each keys number of occurences. Or perhaps there's an easier way of going about it.I could do this:
Return (From kvp As KeyValuePair(Of SomeEnum, Integer) _
In Me.MyObjects Order By kvp.Value Descending _
Select kvp).First().Key
But wouldn't the sorting be a more expensive operation than trying to wiggle Max() in there somehow?
View 1 Replies
Dec 6, 2010
It is possible to output all the dictionary values to a textbox? Not the keys, just the values of all the keys.
View 5 Replies
Oct 5, 2010
I have a collection as follows
Private _bankRates As Dictionary(Of RateSourceBank.Key, RateSourceBank)
Where RateSourceBank.Key is simply
Public Class Key
Public RateType As String
Public EffectiveDate As DateTime
[Code].....
View 1 Replies
Feb 22, 2012
I have this dictoinary:
Dim chardict As Dictionary(Of Char, Integer) = Nothing
chardict.Add("A", 0)
chardict.Add("B", 1)
I wanted to do the following if statement but I am a bit stuck on the syntax:
if chardict.containskey("A")
'display the value that corrosponds to the letter "A"
'in this case display the character 0
[some code]
end if
View 1 Replies
Dec 21, 2010
I have the following in VB:
Dim sources = From source In importSources Select New With _
{.Type = source.Key, .Source = source.Value.Name}
dgridSourceFiles.DataSource = sources
[code]....
View 2 Replies
Aug 31, 2011
I don't know if this is doable, maybe with Linq, but I have a List(Of MyType):
Public Class MyType
Property key As Char
Property description As String
End Class
And I want to create a Dictionary(Of Char, MyType) using the key field as the dictionary keys and the values in the List as the dictionary values, with something like:
New Dictionary(Of Char, MyType)(??)
Even if this is doable, internally it will loop through all the List items, I guess?
View 3 Replies
Sep 22, 2010
Very easy today, I think. In C#, its:
Dictionary<String, String> dict = new Dictionary<string, string>() { { "", "" } };
But in vb, the following doesn't work.
Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) (("",""))
I'm pretty sure there's a way to add them at declaration, but I'm not sure how. And yes, I want to add them at declaration, not any other time. :)
I've also tried:
Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) ({"",""})
And...
Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) {("","")}
And...
Public dict As Dictionary(Of String, String) = New Dictionary(Of String, String) {{"",""}}
View 3 Replies
Mar 11, 2011
I have a dictionary of type:
Dim parentDictionary As Dictionary(int,Dictionary(string,string)).
Dim childDictionary As Dictionary(string,string).
parentDictionary.Add(1,childDictionary)
Now I need to iterate over childDictionary to fetch key value pairs.My childDictionary is inside parentDictionary.
View 1 Replies
May 23, 2009
is there a way i can have a dictionary or something of that sort that can hold 3 values to 1 key?
View 12 Replies
Nov 3, 2009
I can insert values into a VB.NET Dictionary when I create it? I can, but don't want to, do dict.Add(int, "string") for each item.
Basically, I want to do "How to insert values into C# Dictionary on instantiation?" with VB.NET.
var dictionary = new Dictionary<int, string>
{
{0, "string"},
{1, "string2"},
{2, "string3"}
};
View 3 Replies
May 10, 2009
I have a dictionary that has strings variables as keys and a List collection of doubles as values.
Dim CellAddressContainer As New Dictionary(Of String, List(Of Double))
Dim Results As New List(Of Double)
Within a loop I populate the Results variable as follows:
[code].....
View 2 Replies
May 12, 2011
I add objects (structure type) to a dictionary with string keys.
i don't figure out how to update a value of a object.
i use TryGetValue to get a reference to a object of the dictionary collection.
But the changes i made at the referenced object are not made at the object at the dictionary.
Any hints how to get a object reference to change values without to re-add the complete object?[code]...
View 5 Replies
Jan 16, 2012
If I had multiple entries under my value in my dictionary, is there anyway I can put each value in separate tags to create an xml document?
Is there anyway to split the 3 values into their own tags?
View 2 Replies
Jan 20, 2012
I still can't assign the process the username of the session id that is associated with it.[code]...
View 1 Replies
Nov 18, 2009
I have a generic dictonary which is templated in the following manner:
[code...]
If I wanted to omit certain list items against arbitrary keys (that is the items that are in the list contained within the value part of each of the key value pairs making up the dictionary) given some arbitrary condition (lets say omitting list items where a list item contained the string "abc")
I would expect to able to create/project into a new dictionary which would contain entries which contained lists WITHOUT those items whose name contained "abc"
How do I achieve this using a Lambda expression?
I was trying something like this:
[code..]
View 2 Replies
Apr 25, 2009
I have a VB.NET project where I am able to iterate through the keys and values collections of a dictionary object using an index:
MyDictionary.Keys(idx)
MyDictionary.Values(idx)
When this code is taken from the test project and placed into the real project I get the following error:
'System.Collections.Generic.Dictionary(Of Double, String).KeyCollection' cannot be indexed because it has no default property.[code]...
In the line in sub search that says "dtf.Keys(idx) = 0" place your cursor after the right parenthesis and backspace you should get a tooltip that says, "<Extension> ElementAtOrDefault(index as Integer) as Double - index: the zero based element of the index to retrieve.
View 3 Replies
Jan 23, 2012
I have two classes, one nested in the other. [code]Neither "Name" or "ID" are unique between operations and records.I wish to construct a dictionary using LINQ = Dictionary(Of String, Of List(Of Integer), whereby the keys are uniqe examples of Names in my collection and the values are the collective set of distinct IDs that are associated with those names.
View 2 Replies