LINQ And Generic Dictionary?
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
ADVERTISEMENT
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
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
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
Aug 31, 2011
I have a generic class called SessionManager to control the types of variables I store in session. I have been doing it for String, Integer, List(Of T) without any issues like this.
Public NotInheritable Class SessionManager
'''<remarks>
'''Private constructor to prevent instantiation of, or inheritance from, this class.
[code].....
I would like to do the same for Dictionary(Of T, T). I would like 2 generic private methods for Dictionary(Of T, T) for getting and setting; GetFromSessionAsDictionary and SetInSessionAsDictionary. (When nothing returns empty dictionary like the List(Of T) properties and sets in session) And this should allow me to create as many public properties of Type Dictionary (e.g. Dictionary(Of String, String), Dictionary(Of String, Integer)) as possible to store in session.
EDIT:This is what I have ended up doing after trying to make it generic Dictionary(Of T, T), and it works without any issues. What I want instead is a solution that would not constraint the type to Dictionary(Of String, Integer).
Private Const _Dictionary1OfStringInt As String = "DICTIONARY_1_OF_STRING_INT"
Public Shared Property DictionaryOf As Dictionary(Of String, Integer)
Get
[code].....
View 1 Replies
Jul 9, 2009
You can do this
Code:
Dim Test() As String = {"Stuff", "Stuff", "Additional Stuff"}
Is there a way to do that with a generic dictionary? Also, is there a name for that?
View 4 Replies
Mar 16, 2010
I have a mutable class that I'm using as a key to a generic dictionary. Two keys should be equal only if their references are equal. From what I've read, in this case, I don't need to override Equals, GetHashCode , or implement IEqualityComparer.
View 5 Replies
Nov 17, 2009
I have a Generic Dictionary contained or nested as a child in another Generic Dictionary. I need to be able to access the child dictionary objects by key. When I attempt to do this I get the following error:
"Value of type 'System.Collections.Generic.KeyValuePair(Of String, String)' cannot be converted to 'System.Collections.Generic.Dictionary(Of String, String)'."
So it seems that the contained Generic Dictionary is being returned as a Generic KeyValuePair when accessed via a key as opposed to iterating through the parent dictionary in a For/Each loop.
The first section of the sample works OK (using a key to access a string value in a dictionary).
The second section of the code works OK (iterating through the dictionary values in a For/Each).
The third section of the sample is where the error occurs (using a key to access a nested generic dictionary...see underlined code).
Dim td1 As Dictionary(Of String, String)
For Each thisKey As String In td1.Keys
Debug.Print(td1(thisKey))
Next
Dim td2 As Dictionary(Of String, Dictionary(Of String, String))
[Code] .....
View 1 Replies
Feb 19, 2011
I'm having problems binding 2 combo boxes to a generic dictionary, this is what i have done so far
Dim cb As ComboBox = sender
Dim name As String = cb.Name.ToString
Dim dict As New Dictionary(Of String, List(Of String))
[Code]....
View 2 Replies
Apr 19, 2010
I was trying to create a generic Dictionary that implements IXmlSerializable (credit to Charles Feduke).
Here is my trial:
Sub Main()
Dim z As New SerializableDictionary(Of String, String)
z.Add("asdf", "asd")
[Code].....
View 2 Replies
May 20, 2011
If i wish to put items into a System.Collections.Generic.Dictionary, I can either Add or set the Item.I know if you do Add it helps you check if the key already exists and if not it throws an exception.Now I'm curious, so if I'm adding a ton of items, should I prefer setting Item instead of Add since Add does unnecessary checks that may actually slow things down?
View 3 Replies
Apr 2, 2010
Is there any reason to favor one of these approaches over the other when inserting into a generic dictionary with the possibility of a key conflict? I'm building an in-memory version of a static collection so in the case of a conflict it doesn't matter whether the old or new value is used.
If Not mySettings.ContainsKey(key) Then
mySettings.Add(key, Value)
End If
[code].....
View 4 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
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
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
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
Jul 2, 2009
I am trying to learn some VB.NET for my coo-op that starts next week, and for that reason i took my portfolio web site that is on C# and just started to converting it to VB.NET to get familiar with the syntax.
I am sure my problem is simple however i have a hard time solving it. I am trying to grab data with the linq query and then return it as list to bind to the Repeater.I have the following function:
[Code]...
And it would work fine. However if I try to use ToList() in VB.NET i am getting error that it cannot convert it from ling to sql List to the generic list. How do i do this convertion in VB.NET ? or maybe there is another way ?
I am decent when it comes to C# however somewhat lost with the VB.NET syntax.
View 1 Replies
Jun 26, 2011
I need to take a collection and group it via Linq but all the examples I've seen fail in some manner or other with some syntax difference that I can't quite lick.
My collection:
Dim a As New List(Of ProcessAlert)
a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Alert", 2))
a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Document", 2))
[Code].....
View 1 Replies
Nov 22, 2009
I have a function that works great in C# that I'm converting to VB.Net. I'm having an issue converting the result set to a generic list in VB.net.
The code:
Public Function GetCategories() As List(Of Category)
Dim xmlDoc As XDocument = XDocument.Load("http://my_xml_api_url.com")
Dim categories = (From category In xmlDoc.Descendants("Table") _
Select New Category()).ToList(Of Category)()
Return categories
End Function
The error occurs when convertng the result via .ToList(Of Category)() The error:
Public Function ToList() As System.Collections.Generic.List(Of TSource)' defined in 'System.Linq.Enumerable' is not generic (or has no free type parameters) and so cannot have type arguments.
Category is a simple object I've created, stored in the App_Code directory.
I have the necessary "Imports System.Collections.Generic" reference in the file so I don't see why I can't convert the result set to a generic list.
View 2 Replies
Nov 4, 2010
I would like to implement a generic Linq query on a DataGridView.DataSource to retreive a row whith an idField. I did search MSDN, StackOverflow, etc... but didn't quite find what I'm looking for. how I could implement it ?
This is the non-generic code, but I would like to make it work whatever the type of the DataSource is (as long as it implements IEnumerable I guess) and the name and type of the key field.
Dim query = (From note In notesList _
Where note.IdNote = mIdNoteSelectionne _
Select note).FirstOrDefault()
Dim ancienIndex As Integer = notesList.IndexOf(query)
noteList is a List(Of Note), Note is a simple entity class I created, with just members and properties
This is in VB, but feel free to send some C# code, I'll translate it.
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
May 14, 2012
I wrote the following generic LINQ extension for calculating weighted average in Visual Basic 2010:
<Extension()>
Function WeightedAverage(Of T)(ByVal source As IEnumerable(Of T),
ByVal selectorValue As Func(Of T, Integer),
ByVal selectorWeight As Func(Of T, Integer)) As Double
Dim weightedValueSum As Double
Dim weightSum As Integer
[Code] .....
How can I call this function as an Aggregate function of another LINQ query? I tried it in the following way:
Dim q1 = From jd In oContext.JobDatas
Where jd.Year = 2011
Select jd
Dim q2 = Aggregate num In q1 Into WeightedAverage(num.AvSalary, num.NumPosHolder)
The Visual Basic 2010 editor is telling me that the second query (q2) of the following code is not valid statement. On the comma between the first an second argument it's saying: ")" required.
View 1 Replies
May 8, 2009
Dim tenItem = From t In _InvalidFeeList _
From i In ItemCount _
Where t.FeeCode <> i.FeeCode _
[Code]....
I am getting "Can not convert to System.generic.list" error. I look all over for this error and it seem like my code should work for example here
edit: I think i should explain what I'm trying to do here. I want to compare two object list and select the object from the InvalidFeeList if the FeeCode is not equal to the object in ItemCount FeeCode and take the first 10 objects from the InvalidFeeList.
View 2 Replies