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.
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.
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]...
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.
I want to use explicit conversion (CType function in VB.NET) to convert the variable d from double type to integer type and store the result as 132.
Imports System.Console Module Module1
Sub Main()
[CODE]...
This is my code. But Visual Basic tells me an error: Error1Type 'i' is not defined.C:\Users\mihaispr\Desktop\Conversie explicita\Conversie explicita\Module1.vb1922Conversie explicita
i am getting all filenames from a directory.parsing out the directory names & the .vb extension then converting those filesnames to classes.Every file in the LPCriteria direcotry is its own class with a "Evaluate" function.[code]
In MVC I can do something like the following to serialise an object with an anonymous type to JSON.[code]I'd like to do exactly the same but output xml. I haven't been able to find an equivalent method. Would someone please point me in the right direction?
I need to Convert a Ienumerable to a dataset. For this i need to write a common fnction to convert any IEnumberable type to Dataset. For this I need to set anonymous type.[code]...
I'm OK with both C# and VB.NET..I have a function GetListOfBook, that returns LINQ to SQL result that has a collection of objects like the following:[code]
In a VS 2008 ASP.net 3.5 aspx.vb file, Response.Write(DDL.SelectedItem) worked very well.But "Dim filenameDDL As String = DDL.SelectedItem.Text" did not. Then I tried the following codes and found the problem -- Conversion from type 'ListItem' to type 'String' is not valid.
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(?????)
Consider the requirement to always declare Option Strict On. We'll always need to declare variables with the As keyword. What would be the type of an anonymous type?Example : Dim product As ... = New With { Key .Name = "paperclips", .Price = 1.29 }
I have this if statement, which returns the result as a AsQueryable to an anonymous type:
If (signature = "") Then testResults = (From TRTable In context.TestResults Where ((TRTable.Art_no = currentProduct) And (TRTable.Server_time > startDate) And (TRTable.Server_time < endDate))
[code]....
I can't seem to figure out why it isn't working, it was working without the if statement, but I need that functionality.
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
I have code like this which loads a control from an .ascx file, calls some methods of the control, then renders it:
Option Explicit Off
Sub renderMyControl(p As Page) Dim ctrl as Object = p.LoadControl("path/to/myControl.ascx") ' ... do stuff with ctrl ...
[Code]...
This code only works because Explicit is off. I would like to use CType to convert the control to its "strong type" (and get rid of Option Explicit Off) but I don't know how to add a reference to the type. How can I do this?
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,
Ok i'm trying to declare a global list of type T (or is it <anonymous type>) i declare it in a module with something like Friend query As New List(of {whatever type i try}) the app takes some xml and parses it into a list but i need this list available to other classes and methods within the app, everything i have tried fails resulting in an error like Value of type 'System.Collections.Generic.List(Of <anonymous type>)' cannot be converted to 'System.Collections.Generic.List(Of {whatever type i try})'.
What would be the correct way to declare a list of this type? is there another way i could do this?
Just so it's known, this question is mostly academic, even though I tried to use the concept in a real-world solution. I realize the example is contrived, but I believe the concept is valid.I want to write some fluent code like this:
[code]...
I realize that I can't force an anonymous type into a specific type (like implementing an interface or some other class), and I don't want the overhead of defining a specific class just to match my desired fluent name with the actual method name. So I was able to make the code work like this:
copy(my_first_file).to.Invoke(my_second_file)So there is no IntelliSense or type awareness there, and I have to include the Invoke in order to have the method run. How can I get more type safety and exclude the Invoke method, under these constraints: Anonymous Type returned from Method No additional classes or interfaces Preferably, I do not want to pass in another parameter to the copy() method that tells what type to return, unless copy becomes a generic method (but I think that means defining another class/interface, which I don't want to do)
I am developing an application with an Access DB, and i'm having trouble retrieving data from the db, i want to count how many operations have been made through two dates that i'm passing from a datetimepicker, and i can't manage to do it, since i keep getting an error saying "Data type mismatch in criteria expression". In the db, i have that date field type as a date. Before, i had it as a text, but it wouldn't work if i selected one date of x month and the other date from a different month. So, my question is, how do i indicate the type of data in the sql query? [code]