简体   繁体   中英

Linq query from c# to VB.NET

I have this piece of code from a C# project:

public IQueryable<TSource> SearchFor<TSource>(System.Linq.Expressions.Expression<System.Func<TSource, bool>> predicate) where TSource : class {
        var query = (from objects in _dataStore
                    where objects is TSource
                     select objects )
                     .Select(o => (TSource)o).AsQueryable();

        return query.Where(predicate);
}

(_dataStore = private readonly List())

For a new customer I need to create the same function, but now in VB.NET. This is what I tried;

1:

Public Function SearchFor(Of TSource As Class)(ByVal entity As TSource, predicate As Expression(Of Func(Of TSource, Boolean))) As System.Collections.Generic.IEnumerable(Of TSource) 
    Dim a = (From o In mDataContext
            Where o Is entity
            Select o).AsQueryable()

    Dim b = a.Where(predicate) '''<--- Error! Error 3   Overload resolution failed because no accessible 'Where' can be called with these arguments:... And a lot of more text  

End Function

2:

Public Function SearchFor(Of TSource As Class)(predicate As Expression(Of Func(Of TSource, Boolean))) As System.Collections.Generic.IEnumerable(Of TSource) 
    Dim a = From o In mDataContext
            Where o Is TSource ''' <--- Error! 'TSource' is a type and cannot be used as an expression.

            Select o

    Return mDataContext.Where(predicate)
End Function

(mDataContext = As List(Of Object))

4. This link . Gave errors after conversion

I' m out of other options. Maybe some one knows how to fix this?

try this (reference of http://converter.telerik.com/ )

Public Function SearchFor(Of TSource As Class)(predicate As System.Linq.Expressions.Expression(Of System.Func(Of TSource, Boolean))) As IQueryable(Of TSource)
    Dim query = (From objects In _dataStore Where TypeOf objects Is TSourceobjects).[Select](Function(o) DirectCast(o, TSource)).AsQueryable()

    Return query.Where(predicate)
End Function
Public Function SearchFor(Of TSource As Class)(predicate As Expression(Of Func(Of TSource, Boolean))) As IQueryable(Of TSource)
    Dim query = (From objects In _dataStore _
                 Where TypeOf objects Is TSource).Select(Function(o) DirectCast(o, TSource)).AsQueryable()

    Return query.Where(predicate)
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM