Linq Conditional Queries in VB.NET
I am trying to do the same as described here but in VB.NET
I've tried all C # to VB converters but none works with LINQ. I am new to C #, so any help would be appreciated!
public static IQueryable<Type> HasID(this IQueryable<Type> query, long? id)
{
return id.HasValue ? query.Where(o => i.ID.Equals(id.Value)) : query;
}
+1
Phil
a source
to share
2 answers
Try the following:
Public Module MyModule
<Runtime.CompilerServices.Extension()> _
Public Function HasID(ByVal query As IQueryable(Of Type), ByVal id As Long?) As IQueryable(Of Type)
Return If(id.HasValue, query.Where(Function(o) o.ID.Equals(id.Value)), query)
End Function
End Module
In VB.NET, you have to create a module to create an extension method. Also put the Runtime.CompilerServices.Extension attribute and remove the static and this from the function definition.
PS hate VB :)
+3
a source to share
Which converter have you tried? You should try Telerik Converter . I copied your code into my converter and got this:
<System.Runtime.CompilerServices.Extension> _
Public Shared Function HasID(query As IQueryable(Of Type), id As System.Nullable(Of Long)) As IQueryable(Of Type)
Return If(id.HasValue, query.Where(Function(o As ) i.ID.Equals(id.Value)), query)
End Function
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Built and maintained by Todd Anglin and Telerik
'=======================================================
0
a source to share