LINQ strong typing - vb.net

I know LINQ is supposed to automatically return strongly typed result sets. When I attach an ObjectDataSource to a LINQ based method however (with no explicit return type), I don't get access to any of the columns defined in the LINQ.
Example method:
<System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select)> _
Public Function GetMarketClusterList() As IEnumerable(Of MarketCluster)
Return From d In db.tblMarkets
Select New MarketCluster With {.MarketCluster = d.MarketCluster}
Distinct
End Function
Public Class MarketCluster
Public MarketCluster As String
End Class
EDIT
I changed my code to use an explicit type and select into that. At least now I know the return type, but it doesn't help with the original problem. Even weirder, I found with the debugger that if I do
<%# Eval("MarketCluser") %> it fails and says "A field or property with the name MarketCluster was not found on the selected data source", but if I do <% Container.DataItem.MarketCluser %> it works fine!

Simply specify the return type:
Public Function GetMarketClusterList() As IEnumerable(Of MarketCluster)
Return From d In db.tblMarkets
Select d.MarketCluster
Distinct
End Function
(or use IQueryable instead of IEnumerable)
... assuming the type of d.MarketCluster is MarketCluster.
The return type is required if Option Strict is on anyway... which suggests that you've probably not got it on. I would strongly suggest that you enable Option Strict for your project whether you're using LINQ or not, unless you specifically need late binding etc (in which case I'd try to restrict it to only those places that need it).

The error message said the exact error, I just missed it. When it said could not find a "property", it meant class member called "Property". I changed my code to this and now it works perfectly.
Private _MarketCluster As String
Public Property MarketCluster As String
Get
Return _MarketCluster
End Get
Set(value As String)
_MarketCluster = value
End Set
End Property

Related

Type 'Collection' is not defined vb BC30002

I am attempting to migrate a legacy vb.net application to .net standard and turn it into a nuget package. A good amount of it has been straight forward. I am currently hung up on this error caused by functions like this.
Public Property ErrorMessages As Collection
Get
ErrorMessages = _errorMessages
End Get
Set(value As Collection)
_errorMessages = value
End Set
End Property
If i import System.Collections.ObjectModelCollection(Of T) it is asking me for a type and i am unsure how to proceed. It turns my code into
Collection(Of,) and expects a second argument. Has anyone faced this before? Do i use a different import statement or how is this dealt with in vb now?
You should almost certainly replace Collection with Dictionary(Of TKey, TValue), using the dictionary type from the System.Collections.Generic namespace.
Once again, this requires you to fill in the genetic type arguments TKey and TValue with the actual types. You need to figure out from context which type fits the collection. The value of TKey is probably String since that’s the only key type VB6’ collections properly support. And given the name (ErrorMessages), TValue is probably String as well.

Enum with option to show up second describtion

I have some enum inside my code (below) and i am using this enum to show up the names like e.g that: Datasource.Some_server1.ToString(). I am using it either inside code engine to do some calculation if specific server enum is and also to show the name on the webpage. The problem now is my manager asked me to show up diffrent names. So for instance not Some_server1 but for instance: HHGT Server 56. The problem is my code is using those enum names to do some tasks and i cannot just change it within this enum. Do you know some way i can tell inside my project ok now i want see describtion name for this enum so not Some_server1 but now if Datasource.Some_server1.ToString() then show HHGT Server 56. Is there such possibility without not changing my enum in the way rest of code is still using it? Hope you got what i mean.
Public Enum Datasource
Some_server1
Some_server2
Some_server3
Some_server4
End Enum
You can add more context to your Enum by assigning a Description Attribute to each member.
Public Enum DataSource
<Description("HHGT Server 56")> Some_server1
'etcetera
End Enum
Then you can use this function (taken from a really useful extension) to get the Description string:
Public Shared Function GetEnumDescription(ByVal value As [Enum]) As String
Dim fi As Reflection.FieldInfo = value.[GetType]().GetField(value.ToString())
Dim attributes As DescriptionAttribute() = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
Return If((attributes.Length > 0), attributes(0).Description, value.ToString())
End Function

Understanding Array.ConvertAll, can I DirectCast?

I have a base class, DtaRow, that has an internal array of Strings containing data. I have dozens of subclasses of DtaRow, like UnitRow and AccountRow, who's only purpose is to provide Properties to retrieve the values, so you can do aUnit.Name instead of aUnit.pFields(3).
I also have a DtaTable object that contains a Friend pRows As New Dictionary(Of Integer, DtaRow). I don't generally insert DtaRows into the DtaTable, I insert the subclasses like UnitRows and AccountRows. Any given table has only one type in it.
Over in the main part of the app I have an accessor:
Public Readonly Property Units() As IEnumerable
Get
Return Tables(5).pRows.Values 'oh oh oh oh table 5, table 5...
End Get
End Property
This, obviously, returns a list of DtaRows, not UnitRows, which means I can't do MyDB.Units(5).Name, which is the ultimate goal.
The obvious solution is to Dim ret As New UnitRow() and DirectCast everything into it, but then I'm building thousands of new arrays all the time. Uggg. Alternately I could put DirectCast everywhere I pull out a value, also uggg.
I see there is a method called Array.ConvertAll that looks like it might be what I want. But maybe that just does the loop for me and doesn't really save anything? And if this is what I want, I don't really understand how to use DirectCast in it.
Hopefully I'm just missing some other bit of API that does what I want, but failing that, what's the best solution here? I suspect I need...
to make a widening conversion in each DtaRow subclass?
or something in DtaTable that does the same?
You can use ConvertAll to convert an array into a different type.
Dim arr(2) As A
Dim arr2() As B
arr(0) = New B
arr(1) = New B
arr(2) = New B
arr2 = Array.ConvertAll(arr, Function(o) DirectCast(o, B))
Class A
End Class
Class B
Inherits A
End Class
In your case, I think it would look like this
Return Array.ConvertAll(Tables(5).pRows.Values, Function(o) DirectCast(o, UnitRow))
Note that this will create a new array each time.
You can cast the objects into a list(Of String) based on the field you want.
Return Tables(5).pRows.Values.Cast(Of DtaRow).Select(Function(r) r.name).ToList
YES! I went non-linear. This only works because of OOP...
My ultimate goal was to return objects from the collection as a particular type, because I knew I put that type in there in the first place. Sure, I could get the value out of the collection and CType it, but that's fugly - although in C# I would have been perfectly happy because the syntax is nicer.
So wait... the method that retrieves the row from the collection is in the collection class, not the various subclasses of DtaRow. So here is what I did...
Public ReadOnly Property Units() As IEnumerable
Get
Return Tables(dbTblUnits).pRow.Values
End Get
End Property
Public ReadOnly Property Units(ByVal K as Integer) As UnitRow
Get
Return DirectCast(Tables(dbTblUnits)(K), UnitRow)
End Get
End Property
Public ReadOnly Property Units(ByVal K as String) As UnitRow
Get
Return DirectCast(Tables(dbTblUnits).Rows(K), UnitRow)
End Get
End Property
Why does this solve the problem? Well normally if one does...
Dim U as UnitRow = MyDB.Units(K)
It would call the first method (which is all I had originally) which would return the .Values from the Dictionary, and then the Default Property would be called to return .Item(K). But because of the way the method dispatcher works, if I provide a more specific version that more closely matches the parameters, it will call that. So I provide overrides that are peers to the subclasses that do the cast.
Now this isn't perfect, because if I just call Units to get the entire list, when I pull rows out of it I'll still have to cast them. But people expect that, so this is perfectly acceptable in this case. Better yet, when I open this DLL in VBA, only the first of these methods is visible, which returns the entire collection, which means that Units(k) will call the Default Property on the DtaTable, returning a DtaRow, but that's fine in VBA.
OOP to the rescue!

Anonymous Type Collection

How would you solve this? I want to return this collection:
Public Function GetShippingMethodsByCarrier(ByVal Carrier As ShippingCarrier) As List(of ?)
Return Carrier.ShippingMethods.Select(Function(x) New With {.ID = x.ID, .Name = String.Format("{0} {1}", Carrier.Name, x.Description)})
End Function
Thanks!!
You can't return an anonymous type from a function like this because it has no name.
Since this is a public function is should have a well defined return type. Create a new class holding those two properties.
Its possible to return it if the return type is an inferred generic parameter, but that's not what you want here. This is useful for LINQ where an anonymous type essentially gets passed through from a parameter to the result type, but not useful for what you're doing.
You could also use a Tuple, but then you'd lose the property names. And it wouldn't be extensible since adding a new property would break caller code. So I wouldn't recommend that either.
The problem here is you're attempting to return an anonymous type in a strongly typed manner. This is just not possible in VB.Net (or C# for that matter). Anonymous types are meant to be anonymous and their names cannot be stated explicitly in code. The two ways to work around this are to
Option #1 Use / Create a strongly named type like the following
Structure Item
Public ID as Integer
Public Name As String
Public Description As String
End Structure
Option #2 Set the return type to be Object and access the list in a late bound manner
EDIT
As CodeInChaos it is possible to return them in a strongly type manner in a generic context. But that doesn't appear to help you for this particular problem.

What kind of array is Foo() as Foo()?

We generated a class from an XML file a while back. I think we used xsd.exe.
One of the main node collections in the XML file was rendered as:
<System.Xml.Serialization.XmlElementAttribute("PRODUCT")> _
Public Property PRODUCT() As PRODUCT()
Get
Return Me.pRODUCTField
End Get
Set
Me.pRODUCTField = value
End Set
End Property
And sure, there's PRODUCT class defined later on, and it worked fine. Serialized and deserialized fine. Didn't need to worry about it or manipulate it.
Only now we have to revisit and manipulate the data.
But what kind of collection (array?) is Public Property PRODUCT() As PRODUCT(), and how do we loop over it? And add to it?
Basic question, I know. Probably got too comfortable with generics and now xsd has thrown something at me which isn't List(of T) I'm running scared.
Don't be confused by the two sets of parens there. The first set, is simply the parens after the name of the property, whereas the second identifies the return type as an array of Product objects.
Similar to: Public Property IDs() As Integer()
That property returns only an array of integers, and the parens near IDs() only exist because you're declaring the property.
Since it appears to be a standard array of Product objects, you can loop over it with any number of normal loops:
For Each p As PRODUCT In obj.PRODUCTS()
...
Next
or
For i As Integer = 0 To obj.PRODUCTS.Length-1
...
Next i
Your code
Public Property PRODUCT() as PRODUCT()
Returns an array of Objects Of Type PRODUCT. Now whether that Type is a Collection, Structure, or Array I do not know with the code you have provided. The simplest way to loop over it would be as such.
For each prod as PRODUCT in rtnPRODUCTS
'Do Something
Next