Only Access Properties Of An Anonymous Type When I Add ToList To End Of My Linq Query

Dec 16, 2011

I'm learning LINQ and VB and just spent an hour trying to access the fields of an anonymous type defined in a linq query. The key (I learned) is to convert the query to a list before you try to iterate through it with a for loop. How to access property of anonymous type in C#?

This does not work: edit (this compiles, but intellisense does not recognize the type)[code]...

View 1 Replies


ADVERTISEMENT

.net LINQ Xml Query - Anonymous Type?

Oct 22, 2009

I'm out of my depth populating a windows form from an XML linq query (see code further down in post). The listbox is populated as intended, when a value is selected (row) I would like to populate the remaining form controls. The linq query I believe has created a new datatype "Anonymous Types". The query is only local to the load form subroutine but I'm not sure how to make it global.

When I define the query simply as an object the code in the selectedValueChanged event can't execute due to late databinding. how I can initially load the listbox from linq xml query and then populate the other controls when the value changes? Am I right in thinking even though the listbox is multi-columns (property) you can ONLY ever reference a row and NOT a column? I'm new to LINQ and Anonymous Types and the more I read the more confused I seem to get. On a less important note the order by is not functioning as intended (no errors) but data is always retrieved in xml file order and not by the node <text>. Also looked at using <text>.first to order the results without success.

[Code]...

View 5 Replies

ASP.NET MVC - Linq Query With Count Returns Anonymous Type - How To Display In View

Sep 29, 2011

So I'm writing a query as follows:

Dim assSummary = From a In db.Assignments
Join ur In db.UserRegions
On a.Origin.ID Equals ur.Region.ID

[code]....

In the controller I can return the data easily as follows:

For Each c In assSummary
MsgBox(c.Description & " " & c.AssCount)
Next

If I pass the object through to the view using Viewdata("assSummary") = assSummary, how do I display the data? Every method I've tried results in messages about 'VB$AnonymousType_7(Of Integer,String) and I don't know how to retrieve the data from the anonymous type.

View 1 Replies

.net - Linq, VB - Anonymous Type Cannot Be Converted To Anonymous Type?

Jul 9, 2009

I'm a Linq noobie, maybe someone can point me in the right direction. What's wrong here? These anonymous types seem to have the same signatures.

[Code]...

View 2 Replies

.net - Get Properties From Anonymous Type?

Feb 12, 2010

I am trying to figure out how to get at an anonymous typed object's properties, when that anonymous type isn't created in the current function.

Specifically, I am binding an ASP.NET ListView to LINQ resultset, then trying to process each item in the ItemDataBound event.

Option Explicit On
Option Strict On
Class MyPageClass

[Code]....

View 3 Replies

Convert Anonymous Type To Strong Type For Grouping Query?

Aug 30, 2010

I've pieced together some information from other posts but I'm stuck. The first part works fine. Basically I query the database using LINQ and then I loop through the results generating a report.

[Code]...

View 2 Replies

Linq Using Anonymous Type

Nov 12, 2009

Why did the anonymous type property "Points" still have the value "0"? [code]

View 1 Replies

Group By Over Anonymous Type With Linq To Object?

Feb 25, 2010

I'm trying to write a linq to object query in vb.net, here is the c# version of what I'm trying to achieve (I'm running this in linqpad):

void Main()
{
var items = GetArray(

[code].....

View 1 Replies

LINQ - Order By Anonymous Type (Value Element)

Jan 5, 2012

I am using linq to fill a gridview with the information from an xml from code behind. I would like to order my Grid according to one of my elements in the xml ("value element").

gvResourceEditor.DataSource = (From resElem In resourceElements.Elements("data") _
Select New With { _
.Key = resElem.Attribute("name").Value, _
.Value = HttpUtility.HtmlEncode(resElem.Element("value").Value), _
.Comment = If(resElem.Element("comment") IsNot Nothing, HttpUtility.HtmlEncode(resElem.Element("comment").Value), String.Empty) _
}).OrderBy(?????)

View 1 Replies

Cannot Iterate Of A Collection Of Anonymous Types Created From A LINQ Query?

Mar 17, 2010

Every LINQ example I have seen for VB.NET anonymous types claims I can do something like this:

[code]...

Now when I go to iterate through the collection(see example below), I get an error that says "Name "x" is not declared. For Each x in Infos It's like VB.NET doesn't understand that Infos is a collection of anonymous types created by LINQ and wants me to declare "x" as some type. (Wouldn't this defeat the purpose of an anonymous type?) I have added the references to System.Data.Linq and System.Data.DataSetExtensions to my project. Here is what I am importing with the class:

[code]...

View 4 Replies

.net - Pass An Expression To Act On A Single Field In An Anonymous Type In LINQ?

Mar 27, 2012

I have the following Data Transfer Objects defined:

Public Class MemberWithAddressesDTO
Public Property Member_PK As Integer
Public Property Firstname As String
Public Property DefaultAddress As AddressDTO
Public Property Addresses As IQueryable(Of AddressDTO)
End Class

[Code]...

View 1 Replies

Linq To Object: ToList Can Not Convert To Generic List

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

Value Of Type <anonymous Type> Cannot Be Converted To <anonymous Type>

Sep 24, 2011

I am sure i am doing something terribly wrong, but i should better ask the experts.

At the third line i get the error Value of type <anonymous type> cannot be converted to <anonymous type>

Dim Query = (From c In Db.web Select New With {.AA = c.AA}).ToList
Dim v = New With {.Amount = 108}
Query.Add(v)

View 2 Replies

Use Custom Type In A LINQ Query?

Jun 4, 2009

If you have custom type, and you wish to assign a List(of T) to that custom type[code]...

View 1 Replies

Return Type For Collection From LINQ Query?

Oct 15, 2010

I have a method, that returns a group of accounts

Public Shared Function GetAllNotesByUser(ByVal UserID As Guid) As Account (??)
Using db As New MyEntity
Dim query= (From A In db.Account _

[Code].....

I would then like to pass this to another function to calculate the totals for all the accounts in the collection. Is it best practice to return an Ienumerable, a generic list, I'm just not sure what works best with LINQ and the entity framework.

View 4 Replies

.net - In Linq To Entities - How To Do An Inner EXISTS (SELECT 1 FROM - Type Of Query

Mar 6, 2011

I am trying to write a LINQ equivalent of

SELECT C1, C2, C3
FROM T1
WHERE T1.C4='xyz' AND
EXISTS (SELECT 1 FROM T2
WHERE T1.C17 = T2.C24)
ORDER BY C3

I'm using EF CTP 5, so I have a DBContext variable named dbc, which includes DBSet objects T1s and T2s, based on POCOs T1 and T2.

In LINQ I write

DIM IND = From i In dbc.T1s
Where i.C4 = "xyz"
And (From t In dbc.T2s Where i.C17 = t.C24).Any
Select i.C1, i.C2, i.C3
Order By C3

Running the query I get the error message "Unable to create a constant value of type 'T2'. Only primitive types ('such as Int32, String, and Guid') are supported in this context." When I omit the inner expression (third line in the LINQ code), the query runs fine. I tried switching the orders of the inner comparison, to be t.C24 = i.C17, with no effect.

View 1 Replies

Determine The Type Of Object To Use To Store A Linq Query?

Jul 3, 2010

I am using VB .Net for this, so I don't have access to var or this would be a simple matter.Right now my query is as follows

[code]...

So I used this query in LinqPad to help me determine what the object would look like. I got back a IOrderQueryable(Of RSError) which then contained a IGrouping(Of String, RSError) for each grouped collection of objects returned by the query.However I ended up with the current object type of errors as IOrderedQueryable(Of IGrouping(Of String, RSError)) because of the cast error I am getting in VS.

[code]...

I'm not sure how to get rid of the VB$AnonymousType_1 Part of the returned object.Am I even on the right track here or am I missing something completely?

View 1 Replies

How To Use The Vb Equivalent Of ++ For An Index Property In A Linq Query Projecting A New Type

May 3, 2011

I'd normally do this in C# but since I've got to get this code in this particular assembly which is a vb.net one, I'm stuck.

Here's my linq query:

Dim i As Integer = 0

Dim oldAndCurrentIntersectionOnNames = From currentApplicant In currentApplicants _
Group Join oldApplicant In oldApplicants _

[CODE]...

You'll see the .Index = i+=1

This was my attempt to do what I'd quite happily do in C# (i.e. Index = i++) in VB. Unfortunately the VB compiler doesn't like that. how I'd do this in VB.

View 2 Replies

Access The Properties Of All Controls Stored Into Array Of Object Using LINQ?

Jun 3, 2011

I's there a way that I can access the properties of all controls stored into my array of object using LINQ.

Public Sub DisAble(Byval ParamArray ctrlCollection() As Object)End Sub That is my sub routine where im trying to reset all controls that will stored into that object of array. But im trying to attain that using LINQ so I wont use any loops to access all of it.

View 7 Replies

Using Linq And Anonymous Types?

Jun 29, 2009

I have a problem using linq and anonymous types in an asp.net vb.net application.When I write the code above, I have 2 errors :

- "declarations of variable with Option Strinct On requires an As clause"

- "the name 'item' is not declared"

Public Sub MyFunc(ByVal xe As System.Xml.Linq.XElement)
Dim lstitems = From it In xe.Elements Select txt = it.@Text, value = it.@Value
For Each item In lstitems
Dim s As String = item.txt
Next
End Sub

Does it mean that is not possible to use Option Strict On and Option Explicit On ?Or I make a syntax error ?Or is it just a bad configuration of my application ?

View 2 Replies

Using Reflection On Anonymous Types: LINQ To SQL

Mar 19, 2010

I would like to use reflection on an anonymous type resulting from a LINQ to SQL query. I know that reflection on anonymous types works in general. The following code successfully generates a propertyinfo array with two elements:

Dim MyObject = New With {.Col1 = 1, .Col2 = "Test"}
Dim t As Type = MyObject.GetType()
Dim pilist As PropertyInfo() = t.GetProperties

[Code].....

View 7 Replies

.net - LINQ - IEnumerable.Join On Anonymous Result Set?

May 28, 2010

I've long since built a way around this, but it still keeps bugging me... it doesnt help that my grasp of dynamic LINQ queries is still shakey.

For the example:

Parent has fields (ParentKey, ParentField)
Child has fields (ChildKey, ParentKey, ChildField)
Pet has fields (PetKey, ChildKey, PetField)

[Code].....

The above Join call doesnt work. I sort of understand why it doesnt work, but hopefully it'll show you how I tried to accomplish this task.

After all this was done I would have appended a Select to finish the job.

I tried it with the PredicateBuilder with little success. I might not know how to use it right but it felt like it wasnt gonna handle the joining.

View 1 Replies

Distinct Values In LINQ With Anonymous Types

Jul 2, 2011

Supposing the referenced List below contains 2 elements:
Dim Countries = From c In List _
Select New With { .Country = c.Country, .CountryID = c.CountryID }

The code above returns
.Country=Spain .CountryID = 1
.Country=Spain .CountryID = 1

How can I get the distinct values? The Countries query should contain only
.Country=Spain .CountryID = 1

View 5 Replies

Linq Group On Multiple Fields - Anonymous - Key

Apr 3, 2012

I am stumped. I have a DTO object with duplicates patient address data. I need to get only the unique addresses.

[Code]...

View 3 Replies

LINQ Returning List Of Anonymous Types

Mar 11, 2009

Any best approach to what I'm trying to achieve (linq to sql, returning list of data to show in a grid/list etc etc)... Its complaining about anonymous type conversion, and from what I'm reading, thats not elegant way of doing it.

Public Function GetHistory(ByVal historyId As Integer) As List(Of ?????????)
Using dc As New myDataContext(Application.GetConnection)
Return (From t In dc.ActionTypes, a In t.MyTable Where a.HistoryID = historyId Select a.ActionOn, a.ActionBy, t.Description, a.ImpactedItem, a.ActionDescription).ToList
End Using
End Function

View 4 Replies

LINQ: Creating Objects Of Anonymous Types?

Oct 25, 2011

I am in the process of learning LINQ to Objects. I am querying an array of reference elements using LINQ. In the code below, which I have marked with asteriks and PROBLEM, I am having difficulty returning any values for and I suspect its because of the multiple Select arguments.

'Program name:Querying an Array of Invoice Objects
'Created/revised:
'Project description:To demonstrate the use of LINQ to query objects

[Code].....

View 4 Replies

LINQ Query Is Enumerated - Turn On Some Kind Of Flag That Alerts Me Each Time A LINQ Query Is Enumerated?

Sep 22, 2009

I know that LINQ queries are deferred and only executed when the query is enumerated, but I'm having trouble figuring out exactly when that happens.Certainly in a For Each loop, the query would be enumerated.What's the rule of thumb to follow? I don't want to accidentally enumerate over my query twice if it's a huge result.

For example, does System.Linq.Enumerable.First enumerate over the whole query? I ask for performance reasons. I want to pass a LINQ result set to an ASP.NET MVC view, and I also want to pass the First element separately. Enumerating over the results twice would be painful.It would be great to turn on some kind of flag that alerts me each time a LINQ query is enumerated. That way I could catch scenarios when I accidentally enumerate twice.

View 3 Replies

C# - Linq Group By With Anonymous Types Not Working As Expected?

Jul 14, 2011

I was toying around with some of the linq samples that come with LINQPad. In the "C# 3.0 in a Nutshell" folder, under Chater 9 - Grouping, there is a sample query called "Grouping by Multiple Keys". It contains the following query:

from n in new[] { "Tom", "Dick", "Harry", "Mary", "Jay" }.AsQueryable()
group n by new
{

[Code].....

View 1 Replies

Linq Grouping With Anonymous Types And Option Strict

Apr 24, 2012

I've got the below, where I'm grouping a collection by a propery, then wanting to access the properties of my group. Problem I have is with the anonymous typing. Because of option strict I have to give an explicit type but I can't work out what the type should be. The below doesn't compile because t.HeadAccountKey isn't a value (as t has a type of object). So either I need do some casting or my linq selector is wrong,

[Code]....

View 1 Replies

Use Linq To Entities To Group A Collection Without Using Anonymous Types?

Apr 24, 2012

The documentation has an example about grouping on multiple properties[code]...

Is it possible to rewrite the original query to just return IEnumerable(Of CustomerRegionGroup), or do I have to use the anonymous type, and run a second query on the result of the first?

View 1 Replies







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