Enum mapping in LINQ to SQL and VB.NET - vb.net

We have a LINQ-to-SQL datacontext with some int columns we want to map to Enum values. This is in VB.NET and we tried the "Global." prefix trick that works in C#, where global:: maps a field to an enum [see other StackOverflow references on this].
The problem is that SQL metal doesn't generate the code properly for VB.NET and instead creates code like this:
Private _OrderStatusID As [Global].Anvil.Mobile.MobileOrderStates
This won't compile so we have to manually fix by removing the brackets so that the generated code compiles. If we remove the Global. prefix we get the DBML1005 Anyone know a fix/workaround?

Related

EnableCors for VB.net

Does anyone know how to put the enableCors into the controller on vb.net. i am working along with a pluralsight course and tried a code translator with no luck. my attempt is below.
<EnableCors(origins: "http://localhost:53080", headers: "*", methods: "*")>
The correct syntax would be something like this:
<EnableCors("http://localhost:53080", "*","*")>
The C# example appears to use named parameters. VB.NET supports that too, however the EnableCorsAttributes has properties and contractor arguments that only differ by letter casing. This confuses the compiler as to whether you are attempting to set the named parameter or the property in the attribute. So, in this case we can just drop the named arguments all together.
In Vb.net this <EnableCors("http://localhost:53080", "*","*")> will work, but, you have to add on NuGet the Microsoft.AspNet.WebApi.Cors and Microsoft.AspNet.Cors. You need to add Imports System.Web.Http.Cors on the class.
Remove any empty line between the http://localhost:53080", "*","*")> and the declaration of the controller class.

How can I get the display name from my enum

I have researched and it seems that most is bouncing around the problem I have.
#Code
#Imports System.ComponentModel
Dim values = New SelectList([Enum].GetNames(GetType(myEnum)).GetAttribute<DisplayAttribute>()
End Code
The last pararenthesis has a blue line under it and when hover tells me an expression is expected. I want to capture the display name from my enum and have tried many things found on the google search without success. Why am I getting the expression expected error?
Attempted to incorporate and now getting at end parenthesis
Dim type = typeof(MyEnum) ls is expected.
You might want to have a look at this awesome NuGet package called UnconstrainedMelody from Jon Skeet.
https://www.nuget.org/packages/UnconstrainedMelody/
Helpful static methods (or extension methods) for enums and delegates, with constraints which can't be expressed in regular C#.
Have a look at the functions UnconstrainedMelody.Enums.GetNames() and UnconstrainedMelody.Enums.GetValues()

Adding a Dapper Dynamic Output Parameter in VB.NET

I'm trying to use Dapper to call a stored procedure that has a couple Output parameters, using VB.NET (and .NET 4.0).
However, it seems I cannot use the DynamicParameters.Add method, because I'm getting the following compiler error:
'Add' is ambiguous because multiple kinds of members with this name
exist in class 'Dapper.DynamicParameters'.
...when I try to write the following line:
p.Add("#NewRecordID", DbType:=DbType.Int32, direction:=ParameterDirection.Output)
A quick search tells me this sometimes happens when using a C# library that has multiple methods that differ only in name case (VB.NET being case-insensitive). Searching the Dapper source code for DynamicParameters does show the following two overloads for the Add method, but they both use the same case, and the compiler should be able to discern between the two.
public void Add(string name, object value, DbType? dbType, ParameterDirection? direction, int? size)
public void Add(string name, object value = null, DbType? dbType = null, ParameterDirection? direction = null, int? size = null, byte? precision = null, byte? scale = null)
(I've also tried adding scale:=Nothing to the call to force the second overload, to no avail.)
While I can work around this with the input parameters by passing in an anonymous object to the DynamicParameters constructor, I can't find a way around this when adding the output parameters.
I've checked the project references to ensure there aren't multiple or ambiguous assembly references.
Has anybody encountered this problem before, and found a workaround?
At the moment, the only option I can think of is to re-write the stored procedure call without using Dapper, as implied here.
From what I can gather, the following are all potential solutions:
Rewrite the Stored Procedure to not use Output parameters. (The option I was able to use in this case.)
Rewrite the code calling the Stored Procedure to use standard ADO.NET.
Rewrite Dapper to use a different overload pattern for DynamicParameters.Add.
Update the project to use .NET 4.5.
Reimplement IDynamicParameter(s) or possibly subclass DynamicParameters

ComAutomationFactory

Im trying to use the ComAutomationFactory class in VB .NET the example im working from is c# and is working fine but my project doesnt compile and says this class is not defined??
also what is the return type? in the c# example it returns a type of dynamic but this type does not exist in vb .NET?
Possibly you're sample is old, according to this blog:
http://silverlight-essentials.blogspot.com/2010/03/breaking-changes-in-com-interop-between.html
It's changed name as:
The class ComAutomationFactory has been renamed to AutomationFactory.
Additionally you have to reference System.Runtime.InteropServices.Automation for this class.
Regarding the return type, the answer to this question has a solution:
Iterating over Word Document Fields using ComAutomationFactory in Silverlight 4

VB.net can't find by string

Using VB.net, the following snippet gives the error below.
Dim _account = Account.Find(Function(x As Account) x.AccountName = txtFilterAccountName.Text)
or similarly if I do
.SingleOrDefault (Function(x As Account) x.AccountName = txtFilterAccountName.Text)
will both give the error "The method 'CompareString' is not supported". If I make the same call searching for an integer (ID field) it works fine.
.SingleOrDefault (Function(x As Account) x.Id = 12)
So integer matching is fine but strings don't work Is this a problem with the VB.net templates?
No this is not a problem with Vb.Net templates.
The problem is that you are not using a normal LINQ provider. Based on your tag (subsonic) I'm guessing you're using a LINQ to SQL query.
The problem is that under the hood, this is trying to turn your code into an expression tree which is then translated into an SQL like query. Your project settings are turning your string comparison into a call in the VB runtime. Specifically, Microsoft.VisualBasic.CompilerServices.Operators.CompareString.
The LINQ2SQL generater in question or VB compiler (can't remember where this check is done off the top of my head) does not understand how to translate this to an equivalent bit of SQL. Hence it generates an error. You need to use a string comparison function which is supported by LINQ2SQL.
EDIT Update
It looks like the CompareString operator should be supported in the Linq2SQL case. Does subsonic have a different provider which does not support this translation?
http://msdn.microsoft.com/en-us/library/bb399342.aspx
The problem is with SubSonic3's SQL generator and the expression tree generated from VB.NET.
VB.NET generates a different expression tree as noted by JaredPar and SubSonic3 doesn't account for it - see Issue 66.
I have implemented the fix as described but it has yet to merge into the main branch of SubSonic3.
BlackMael's fix has been committed:
http://github.com/subsonic/SubSonic-3.0/commit/d25c8a730a9971656e6d3c3d17ce9ca393655f50
The fix solved my issue which was similar to John Granade's above.
Thanks to all involved.