[英]Linq query from c# to VB.NET
我从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))
这个链接 。 转换后给出错误
我没有其他选择。 也许有人知道如何解决这个问题?
试试这个(参考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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.