gpt4 book ai didi

c# - 从 C# 到 VB.NET 的 Linq 查询

转载 作者:太空狗 更新时间:2023-10-30 01:02:55 25 4
gpt4 key购买 nike

我有一段来自 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 .转换后报错

我别无选择。也许有人知道如何解决这个问题?

最佳答案

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

关于c# - 从 C# 到 VB.NET 的 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31741297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com