Return Anonymous type from LINQ query in VB.NET - asp.net

I am consuming an RSS feed to display on my website using a repeater control. I was wondering if it's possible in VB to return an anonymous type from my linq query rather than a collection of strongly typed RSSItems. I know this is possible in C#, however haven't been able to work out a VB equivalent.
Public Class RSSItem
Public Property Title As String
Public Property Link As String
Public Property Content As String
Public Property Description As String
Public Property pubDate As String
Public Property category As String
End Class
Dim feedXML As XDocument = XDocument.Load("http://myrssfeed.com/rss.xml")
Dim xns As XNamespace = "http://purl.org/rss/1.0/modules/content/"
Dim feeds = From feed In feedXML.Descendants("item") _
Select New RSSItem With _
{.Title = feed.Element("title"),
.Link = feed.Element("link"),
.Content = feed.Element(xns.GetName("encoded")).Value,
.Description = feed.Element("description"),
.pubDate = feed.Element("pubDate"),
.category = GetCategories(feed.Elements("category"))}

I believe you can change New RSSItem With to New With. More details can be found in the VB Anonymous Types MSDN page.

Related

difficulty serializing a collection in web api 2 using knockoutjs

I have a knockoutjs web page doing a post to a VB .net Web API. for some reason my collection always shows nothing on the controller.
on the web page in the post here is my data.
data: ko.toJSON({
ShpmntCntrlNbr: self.ShpmntCntrlNbr,
MstrBillNbr: self.MstrBillNbr,
accesorialChangesHaveBeenMade: self.accesorialChangesHaveBeenMade,
DB2ACTCollection: actCollection
}),
this shows in the console (chrome developer tools) like this.
{
"ShpmntCntrlNbr":"11019813",
"MstrBillNbr":" ",
"accesorialChangesHaveBeenMade":true,
"DB2ACTCollection":[
{"ShpmntCntrlNbr":"11019813"}
]
}
here is .net VB controller
Public Function Post(data As VMFreightReleaseSaveDB2ChangesInput) As IHttpActionResult
Return Ok()
End Function
unfortunately when I put a stop sign in here and inspect data. I get
DB2ACTCollection Nothing
MstrBillNbr " "
ShpmntCntrlNbr "1101983"
accesorialChangesHaveBeenMade True
no clue why my DBACTCollection is showing as nothing. any thoughts?
and here are the classes.
Public Class VMFreightReleaseSaveDB2ChangesInput
Public Property ShpmntCntrlNbr() As String
Get
Return m_ShpmntCntrlNbr
End Get
Set(value As String)
m_ShpmntCntrlNbr = value
End Set
End Property
Private m_ShpmntCntrlNbr As String
Public Property MstrBillNbr() As String
Get
Return m_MstrBillNbr
End Get
Set(value As String)
m_MstrBillNbr = value
End Set
End Property
Private m_MstrBillNbr As String
Public Property accesorialChangesHaveBeenMade() As Boolean
Get
Return m_accesorialChangesHaveBeenMade
End Get
Set(value As Boolean)
m_accesorialChangesHaveBeenMade = value
End Set
End Property
Private m_accesorialChangesHaveBeenMade
Public Property DB2ACTCollection() As List(Of DB2_ACT2)
Get
Return m_DB2ACTCollection
End Get
Set(value As List(Of DB2_ACT2))
value = m_DB2ACTCollection
End Set
End Property
Private m_DB2ACTCollection As List(Of DB2_ACT2)
and
Public Class DB2_ACT2
Public Property ShpmntCntrlNbr() As String
Get
Return m_ShpmntCntrlNbr
End Get
Set(value As String)
m_ShpmntCntrlNbr = value
End Set
End Property
Private m_ShpmntCntrlNbr As String
End Class
Just a note here is my solution so far, not real happy with it but it seems to be working. instead of data As VMFreightReleaseSaveDB2ChangesInput I have data as JToken. then I just loop through it although I am still at a loss why the native .net web api serializer is not doing this.
Dim inner As JArray = data("DB2ACTCollection")
Dim ACTCollection = New List(Of DB2_ACT)
For Each item As JObject In inner
Dim ACT As New DB2_ACT
ACT.ShpmntCntrlNbr = item("ShpmntCntrlNbr")
ACTCollection.Add(ACT)
Next
Based on the JSON and seeing as the models are POCOs try using auto properties with DB2ACTCollection as an array and see if that deserializes properly
Public Class DB2_ACT2
Public Property ShpmntCntrlNbr As String
End Class
Public Class VMFreightReleaseSaveDB2ChangesInput
Public Property ShpmntCntrlNbr As String
Public Property MstrBillNbr As String
Public Property accesorialChangesHaveBeenMade As Boolean
Public Property DB2ACTCollection As DB2_ACT2()
End Class

Convert a standard EF list to a nested JSON

I have a WEB Method in my ASPX page that retrieves a List from SQL DB using Entity Framework.
Using rep As New RBZPOSEntities
q = rep.getSalesByLocation(fDate, tDate).ToList
End Using
Thereafter i use javascriptserializer to convert this list into a JSON string
Dim jss As JavaScriptSerializer = New JavaScriptSerializer()
Dim json As String = jss.Serialize(q)
So the above works great and i can use AJAX on the client side to display the results successfully.
The problem i am having now is converting a Flat List into a Nested JSON string.
So consider a list like:
locationName as string
MonthName as string
totalAmount as string
Which needs to be converted into a JSON like this:
[{locationName:'Tokyo',totalAmount:[100,200,300,400]},
{locationName:'New York',totalAmount:[500,600,700,800]}]
So the totalAmount values in the above case correspond to totalAmounts for a Location for a specific month. E.g. Tokyo total amount in January is 100, in February is 200 and etc.
What i can do:
I can create a nested list and populate it with results from EF and then serialize to JSON.
What i am asking:
Is there any other cleaner way to do it.
Thank you
As Todd has already said you need to convert your flat list into a list of another intermediate type. Let's call your existing Type the "InputType" and the Type you want the "OutputType"
Public Class InputClass
Public locationName as String
Public MonthName as String
Public totalAmount as String
End Class
Public Class OutputClass
Public LocationName As String
Public TotalAmount As List(Of String)
Public Sub New(groupedInputClass As IGrouping(Of String, InputClass))
LocationName = groupedInputClass.Key
TotalAmount = groupedInputClass.Select(Function(x) x.totalAmount).ToList()
End Sub
End Class
Then all you need to do is transform a List of InputType into a List of OutputType which is really easy with LINQ.
Public Class MappingHelper
Public Function Map (inputList as List(Of InputClass)) As List(Of OutputClass)
Dim groupedByLocation = inputList.GroupBy(Function(k) k.LocationName)
Dim nestedLocations = groupedByLocation.Select(Function(x) new OutputClass(x)).ToList()
Return nestedLocations
End Function
End Class
If you use an intermediate type, it may be helpful. Something like:
class RbzposInfo {
public string Location {get;set;}
public List<MonthlyRevenue> Monthlies {get;set;}
}
class MonthlyRevenue {
public string Month {get;set;}
public int Revenue {get;set;}
}
var result = new List<RbzposInfo>();
foreach (var r in q) { // q from your code
var info = new RbzposInfo();
// TODO: put r values into info.
result.Add(info);
}
Dim json As String = jss.Serialize(result);
(PS, sorry I mixed .NET languages on you. My code is C#.)

VB.NET hide a property member

I am currently working on the return class. The problem is I want to show the certain member only when some of the condition meet. Below is my code. I only want to show ResponseMsg member when the ResponseCode is 99 otherwise it will be hidden.
Public Class LoginResponse
Public Property TerminalID As String
Public Property ReaderID As String
Public Property TransRef As String
Public Property TransDateTime As String
Public Property Timeout As Integer
Public Property ResponseCode As String
Public Property ResponseMsg As String
Public Property Cryptogram As String
End Class
You can't that I know of. But you can do something like this:
Public Property ResponseMsg
Get
If ResponseCode <> SomeCodeValue
Return _responseCode
Else
Return Nothing
End if
End Get
End Property
You might want to think about making a specialized class.
Let's say you have your basic LoginResponse
Public Class LoginResponse
Public Property TerminalID As String
Public Property ReaderID As String
Public Property TransRef As String
Public Property TransDateTime As String
Public Property Timeout As Integer
Public Property ResponseCode As String
' Note: no ResponseMsg here
Public Property Cryptogram As String
End Class
Then you'd have an extended response class inheriting your basic LoginResponse:
Public Class LoginResponseEx : Inherits LoginResponse
Public Property ResponseMsg As String
End Class
Then where ever you create those LoginResponse objects, you just create one of the apropriate.
Let's say you have a GetResponse() procedure like:
Public Function GetResponse() As LoginResponse
Dim result As LoginResponse = Nothing
Dim code As Integer = GetSomeCode()
' ... get the other properties
' Say you have a const or something with the appropriate code: SPECIAL_CODE
If code = SPECIAL_CODE Then
Dim msg As String = GetSomeMessage()
result = New LoginResponseEx(..., code, msg, ...) ' have a special Response
Else
result = New LoginResponse(..., code, ...) ' have a normal Response
End If
Return result
End Function
Finally when checking the response you just check whether you have a special value in ResponseCode and cast the object respectivly.
'...
Dim resp as LoginResponse = GetResponse()
If resp.ResponseCode = SPECIAL_CODE Then
Dim respx as LoginResponseEx = CType(resp, LoginResponseEx)
Console.WriteLine("ResponseMessage was: " & respx.ResponseMsg
Else
Console.WriteLine("No ResponseMessage")
End If
'...
This way you have your basic LoginResponse with the ResponseMsg hidden in the special class ResponseLoginEx
Note when you do this you should think about how you implement virtual classes. e.g. the fields might have to be declared as Protected instead of Private, though i'm sure you'll do fine.
This also works with Serializable classes, of course.

Deserialize JSON in ASP.NET with VB

I'm using VB in ASP.NET and I've been looking at trying to deserialize the below JSON for about 4 days now with no success.
The problem is the code I have below is returning null values. I would like to get results of the JSON for each class member I've declared including price and shipping values for each product.
Can anyone point me in the right direction with regard to
1.) why I keep getting null values back and
2.) if the classes I declared are valid for the json I am trying to deserialize?
Any help is greatly appreciated!
Here is an example of the JSON I am working with:
{
"kind": "shopping#products",
"items":
[{
"kind": "shopping#product",
"id": "tag:google.com,2010:shopping/products/8040/8382012077897342942",
"product": {
"googleId": "8382012077897342942",
"title": "LEGO Star Wars™: Jabba's Palace™ (9516)",
"description": "Rescue Han Solo from Jabba the Hutt's desert palace!",
"inventories": [
{
"price": 119.99,
"shipping": 12.95,
"currency": "USD"
}
]
}
}
]
}
Here is the Code I have currently:
Imports System.Web.Script.Serialization
Imports Newtonsoft.Json.Linq
Public Class inventories
Public Property price As Double
Public Property shipping As Double
End Class
Public Class product
Public Property googleid As String
Public Property title As String
Public Inventories As inventories()
End Class
Public Class Items
Public Property product As product()
Public Property kind As String
Public Property id As String
End Class
Partial Class JSON_Test
Inherits System.Web.UI.Page
Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click
' 1. Get JSON string from Google
Dim google_json_string As String
google_json_string = json_text.Text
'2. Deserialize JSON - Method 1
Dim jss As New JavaScriptSerializer
Dim ds_results = jss.Deserialize(Of List(Of Items))(google_json_string)
result1.Text = ds_results.Count
result2.Text = ds_results(0).kind
End Sub
End Class
(Update) I've updated the code to include classes that are structured like this (thanks Dan-o):
Public Class g_JSON
Public Property items As List(Of Items)
End Class
Public Class Items
Public Property product As product()
Public Property kind As String
Public Property id As String
End Class
Public Class product
Public Property googleid As String
Public Property title As String
Public Inventories As inventories()
End Class
Public Class inventories
Public Property price As Double
Public Property shipping As Double
End Class
I also updated the code to read as follows:
Partial Class JSON_Test
Inherits System.Web.UI.Page
Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click
' 1. Get JSON string from Google
Dim google_json_string As String
google_json_string = json_text.Text
'2. Deserialize JSON - Method 1
Dim jss As New JavaScriptSerializer
Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string)
result1.Text = ds_results.items(0).id
result2.Text = ds_results.items(0).kind
End Sub
End Class
Now, the code will compile without issue but when I kick off the click event I get the following error:
No parameterless constructor defined for type of 'product[]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for type of 'product[]'.
Source Error:
Line 38: Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string)
What does that mean and how do I create a parameterless constructor for product()?
Thanks for taking a look!
According to jsonlint your json isn't valid (comma after "inventories" array).
You forgot the container.. the class that holds everything...
Class something
Public property kind as string = String.Empty
Public property items as list(of Item) = Nothing
End Class
Then you deserialize to something, not list(of item)

Binding arraylist to grid view (Part whatever....)

Ok, I'me getting real dumb with this one....
I have this class:
Public Class whatever
Public id as string
Public name as string
public date as string
end class
Wich I use with this code:
dim personlist as new arraylist
dim person as new whatever
person.id="1"
person.name="bozo"
person.date="6-6-6"
personlist.add(person)
and then i repeat so that i can fill my arraylist with all the information that I want to show in my gridview.
The problem is this:
gridview1.datasource = personlist
gridview1.databind()
When executing, I get an error saying :
The data source for GridView with id 'gdpersonlist' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.
Can anyone help me or perhaps point me in the right direction to do this?!
Try using properties instead of fields. The data binding for the grid view won't work for fields.
Public Class whatever
Public _id as string
Public name as string
public date as string
Public Property Id As String
Get
Return _id
End Get
Set (value as String )
_id = value
End Set
End Property
' repeat for all 3 fields
end class

Resources