Returning Subset Of Dictionary With A Complex Type As A Key With Linq

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


ADVERTISEMENT

.net - Filter Custom Dictionary With LINQ ToDictionary - "Unable To Cast Object Of Type 'System.Collections.Generic.Dictionary`2"

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

VS 2008 : Randomize Dictionary Of Complex Types?

Jun 29, 2009

I wrote the following function to randomize a dictionary object:

Friend Function RandomizeDictionary(Of T)(ByVal oCollection As Dictionary(Of String, T)) As Dictionary(Of String, T)
Try
Dim oResult As Dictionary(Of String, T) = Nothing

[code]....

This code works fine on simple dictionary objects, but when I use it with complex types I get a type cast exception. For example, I have an object X that Inherits Dictionary (Of String, Class Y). If I call the randomize function on object X, I get the type cast exception. I don't really understand why it cares what the type of the dictionary value is, I thought that was the point of using generics.

In summary the following all work:

Dim x as New Dictionary (Of String, String)
Dim x as New Dictionary (Of String, Boolean)
Dim x as New Dictionary (Of String, Integer)

This does not:

Dim x as New Class X
Class X Inherits Dictionary (Of String, Class Y)

View 5 Replies

Use Linq ToDictionary To Return A Dictionary With Multiple Values In The Dictionary Items?

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

.Net Linq - Doing Operation On The Subset

Jun 16, 2010

I need to have the following : (name1 + "a") + (name2 + "a") + ...

Dim separator() As String = {"|"}
myString.Split(separator, StringSplitOptions.None).SomeLinq(...)

I don't know what to add at the end to add an "a" to each element...

View 2 Replies

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

LINQ To XML To Create Complex XML File?

Feb 9, 2012

I would like to know if there is any simple/fast way to create a complex XML file, when I say complex, I mean more than 20 elements encapsulated. Although this is VB.net,unfortunately literals will not work in this case

View 2 Replies

.net - Generate Complex Object From LINQ To XML In .NET?

Apr 1, 2009

I have an XML File that I am processing using LINQ. I want to basically serialize the XML data into custom objects but don't know how.

[Code]...

How can I finish the above LINQ query to populate the Children property of the Group object?

View 2 Replies

Sql Server - Converting Complex Sql Stored Proc Into Linq

Jan 24, 2011

I'm using Linq to Sql and have a stored proc that won't generate a class. The stored proc draws data from multiple tables into a flat file resultset. The amount of data returned must be as small as possible, the number of round trips to the Sql Server need to be limited, and the amount of server-side processing must be limited as this is for an ASP.NET MVC project. So, I'm trying to write a Linq to Sql Query however am struggling to both replicate and limit the data returned. Here's the stored proc that I'm trying to convert:

[Code]....

View 1 Replies

Use LINQ To Find Complex Combinations Of Items In Two Lists?

Jun 28, 2010

This question is very similar to a previous question of mine, Use LINQ to count the number of combinations existing in two lists, except with some further twists.

I have a list of CartItems that can receive a discount based on the items specified in the list of DiscountItems. I need to be able to pull out the items in the Cart that can receive a discount and apply the appropriate discount specfied in the DiscountItem. The discount is only applied for each combination that exists. Here's what the two lists might look like to before the discount is applied[code]...

View 3 Replies

MVC - Rendering Textbox For Complex Type?

Nov 2, 2011

I have been reading the various blog posts on how to deal with a Complex Type. However I don't really get it. I am using EF code first development, MVC and VB. From what I have read so far to render a editor field for a complex types requires a custom object, right? However I don't really understand what code I need to put into the custom template. What code needs to go into custom template so I can render a textbox for the PostTags icollection?

My classes:
Public Class Post
Inherits EntityBase
<Key()> Property PostId As Integer
<DisplayName("Title")> <Required()> Property PostTitle As String
<UIHint("MultilineText")> <DisplayName("Text")> Property PostText As String
Code] .....

View 1 Replies

.net - Sorting A Dictionary By Value With Linq?

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

How To Query Dictionary(OF T , T) By LINQ

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

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

LINQ Query On A Dictionary?

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

VB And LINQ Query On A Dictionary

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

.NET And LinQ: How To Delete Entries From A Dictionary Using The Value

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

Linq - .NET Order Dictionary Alphabetically?

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

Returning In LINQ To XML?

May 23, 2011

I am working with a method using LINQ to XML to return a string.This is the XML

<data name="lnkViewResultResource1.Text" xml:space="preserve">
<value>View results</value>
</data>

[code].....

View 3 Replies

Avoid For Loop (Dictionary Array) Using LINQ?

Nov 23, 2010

How to avoid for loop (Dictionary Array) using LINQ

View 1 Replies

Get The Corresponding Key For The Maximum Value In A Dictionary(Of SomeEnum, Integer) Using LINQ?

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

Asp.net - Complex Linq Query - Add A .Where() Or .Any() Predicate To Reduce Query Complexity (in VB)?

Sep 14, 2010

I have to join two main tables, and I need to filter the results by elements in an ASP.NET web form. These filters are created on the fly so I have to use a lot of where extensions to filter the query. I want to execute the query with as optimized SQL as possible.

I am first doing a simple join between TW_Sites and TW_Investigators. Then there are two sub-tables that are involved. TW_InvestigatorToArea and TW_InvestigatorToDisease. While most of the where clauses are working fine, I have found a performance issue that won't be an issue right now, but will be an issue as the table gets bigger.

The arrays DiseaseCategories and DiseaseAreas would be the results of a CheckBoxList result.

Protected Sub LoadResults()
'Get Dictionary of Filters
Dim FilterDictionary As OrderedDictionary = Session.Item("InvestigatorFilterDictionary")
' Initialize LinqToSql

[code]....

View 2 Replies

.net - Linq Query Not Returning IEnumerable?

Jan 22, 2010

I have the follow Linq query that is in a web application that was converted from .NET 1.1 to 3.5:

dim objListOfFilteredDataRows = from datarows as datarow in objDataSet.tables(0).rows _
where datarows("SomeColumn") = SomeValue

I have the exact same query in an application that was created using .NET 3.5 and the query returns an IEnumerable. However the query in the converted application is returning:

{Name = "WhereEnumerableIterator`1" FullName = "System.Linq.Enumerable+WhereEnumerableIterator`1[[System.Data.DataRow, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"}

**Edit: When I highlight the expression, the intellisense says that it doesn't know the type of objListOfFilteredDataRows and assumes its a type of "Object". Why is the type not infered in the converted 1.1 application but is infered in the "native" 3.5?**

What am I missing here? How do I convert the "WhereEnumeratorIterator`1 to an IEnumerable?

View 5 Replies

Asp.net - LINQ-to-SQL Query Returning No Results?

Mar 5, 2011

I have a query using LINQ-to-SQL. It queries an underlying database table Rooms. It uses Where conditions to narrow down the results, namely:

Gender.Current Occupancy < Max Occupancy Available Flag is Checked I know this should return results but it keeps returning an empty set.Code is below

[code]...

UPDATE: I've verified that the issue is with the statement where sh.is_available = 1, which doesn't make sense since this is a bit field.

View 3 Replies

Linq Query Returning Value From Another Class?

Apr 28, 2011

Just a quick question about LINQ, I want to use a value from the returned dataset to lookup a value and return this. The line I am struggling with is .ViewingNotes = NewViewing(pt.ProspectId).GetViewings().Columns(7).ToString(). Is this possible?

With BusinessLayerObjectManager.Context
Return (From p As [Property] In .PropertySet
Join pt As Prospect In .Prospects On pt.Property.propertyID Equals p.propertyID

[code].....

View 1 Replies

LINQ To XML Query Returning No Results?

Jul 20, 2011

I'm doing some XLINQ in VB for work. I basically need to pull some values from a small chunk of XML as listed here:

<?xml version="1.0" encoding="utf-8"?>
<Fields>
<typeQtyRadioButtonList>1</typeQtyRadioButtonList>
<cmbQtyCheck>Reject</cmbQtyCheck>

[code]....

... Leaving out the implementation of the For Each loop....So I have stuck a break point on the for each and the collection has no elements in it.

View 1 Replies

VS 2008 LINQ Not Returning Results?

Mar 3, 2010

So, I'm not really sure if I have this setup properly or not. This is my first attempt at utilizing LINQ and I am trying to return data directly from an XML file.Here is my XML structure:

<folder_list>
<folder lbl="_localhost" abs="C:\_localhostapache2triadhtdocs" unc="C:\_localhostapache2triadhtdocs" tag="default" />
<folder lbl="Programming" abs="D:\_prog" unc="D:\_prog" tag="default" />
<folder lbl="Anime" abs="D:Anime" unc="D:Anime" tag="default" />

[Code]...

View 9 Replies

Linq Query On Dictionary<TKey, TValue> Working As DataSource?

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

Linq To Objects - (C#) Creating A Dictionary From An Existing List Without Looping?

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

Linq Expression Returning Null Rather Than Empty Set

Nov 18, 2010

I am finding this weird issue. When I do this > activities.Where(Function(a) (Not a.IsDeleted And a.ParentId = 100) It returns an in-memory query & when I try opening it up, it throws a object not set exception. This only happens when there were no items which satisfied the condition. Shouldn't it be returning an empty set? When there are items satisfying the condition, then it returns a list & works all good.

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved