Use LINQ To Omit Some Entries In The Value Part Of A Dictionary And Project This Into A New Dictonary Maintaing Original Keys
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
ADVERTISEMENT
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
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
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
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
May 31, 2011
I have a dictionary(of someType, someOtherType) (the types are immaterial). With a given value, I need to find all the keys that have that value in the dictionary. What I am currently doing is this:
Dim foundList as New List(of someType)
For each key as someType in myDictionary.Keys
If myDictionary(key) = givenValue Then
[code]....
View 10 Replies
Apr 5, 2012
There a dictionary object that gets loaded with the following key values:
[Code]...
In most situations, life is good and all the individual key values are needed/unique. But in certain situations, the keys with letters behind them (ie...189a, 189b, 189c) all mean the same thing (ie...189). So I need a way to see if a key value exists (like the containskey method) for only the first part of the key and then return true.
View 2 Replies
Nov 10, 2010
I have the following dictionary:
Dim idQueuedJobs As IDictionary(Of Int32, KeyValuePair(Of String, Int32)) = New Dictionary(Of Int32, KeyValuePair(Of String, Int32))
Why duplicate KeyValue pair? This is because the first Int32 is just a normal index, and the String followed by another Int32 contains systemnames and the priority of the qued job in idQueuedJobs
I want to calculate the total number of systemnames in idQueuedJobs, how can i obtain this count by using the groupby method?
Something like this maybe?
numberOfSystems As Int32 = idQueuedJobs.Values.GroupBy(...)
View 1 Replies
Aug 3, 2009
I have this dictionary..
Dim Rooms As New Dictionary(Of Integer, Of Integer)
Rooms(1) = 101, 102, 109, 110
Rooms(2) = 103, 104, 105
Rooms(3) = 106, 107
I want to know if i can obtain how many keys are in the dictionary For example, in this dictionary i have 3 keys, and if i use Rooms.Count it returns me 9 thats each pair of keys-values, but i want to obtain 3, each diferent value as key.
EDIT: Sintax error P.D: I cant use now because i have to use in a webservice filtered by IP, but VS2010 if i use count on the dictionary, tells me that will contain the number of the keys/value pairs.
View 1 Replies
Jun 9, 2012
I have this dictionary..
Dim Rooms As New Dictionary(Of Integer, Of Integer)
Rooms(1) = 101, 102, 109, 110
Rooms(2) = 103, 104, 105
Rooms(3) = 106, 107
I want to know if I can obtain how many keys are in the dictionary. For example, in this dictionary i have 3 keys, and if I use Rooms.Count it returns me 9 that's each pair of keys-values, but I want to obtain 3, each different value as key. Syntax error. I cant use now because I have to use in a webservice filtered by IP, but VS2010 if I use count on the dictionary, tells me that will contain the number of the keys/value pairs.
View 3 Replies
Oct 7, 2010
I have a dictionary with the keys being integer and the values being string. I was wondering how can I sort the dictionary in DESCENDING order using its keys.
View 6 Replies
May 31, 2012
I have a case insensitive dictionary in asp.net/vb.net like this:
Dim caseInsensitiveDictionary = New Dictionary(Of String, Single)(StringComparer.OrdinalIgnoreCase)
it holds values like this
[code].....
View 2 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
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
Feb 16, 2009
if I have an image in the dictionary that is pulled in from the RESX file..... my code replaces some if a name match occurs. imgDct(dKey) = Image.FromFile(aFi.FullName) so, now the dictionary has a file pulled in. it seems that this LOCKS the original file? so i cant edit it.. and replace it is there a way i can pull the image in without "Locking" the original?
View 11 Replies
Dec 2, 2009
I am trying to sort a LINQ to SQL query based on two fields. The first field is occasionally null which automatically sorts to the top of an ascending query. Is there any way to make the null entries sort to the bottom?
Here is an example:
From x in SampleDataContext.Event _
Order By x.Date, x.Sequence_Number _
Select x.Date, x.Sequence_Number
[code]....
View 1 Replies
Oct 24, 2010
If I use a LINQ query over some type of collection, is there any way to know the initial index of an element of the result?
Purpose: I make a query to set as source of a DataGridView, and on a (button)cellclick event I want to a call a method that needs to reference the collection at the correct index.
View 13 Replies
Aug 9, 2009
I'm using VB .NET and I know that Union normally works ByRef but in VB, Strings are generally processed as if they were primitive datatypes.Consequently, here's the problem:
Sub Main()
Dim firstFile, secondFile As String(), resultingFile As New StringBuilder
firstFile = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "1.txt").Split(vbNewLine)
secondFile = My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.Desktop & "2.txt").Split(vbNewLine)
[Code]...
View 2 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
Apr 2, 2011
I'm using Microsoft Visual Studio 9... Which uses Visual Studio 2008.I would like to re-work a previous project without changing the original to which end I'd like to make a copy of the original, with a different name obviously.
View 2 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