Linq查询从c#到VB.NET

时间:2015-07-31 08:13:28

标签: c# vb.net linq

我从C#项目获得了这段代码:

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())

对于新客户,我需要创建相同的功能,但现在在VB.NET中。这就是我的尝试;

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。转换后给出错误

我没有其他选择。也许有人知道如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

试试这个 (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

答案 1 :(得分:2)

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