0

我想将我的一些 EntityFramework C# 代码移植到 VB.NET 中。我正在尝试不同的在线代码转换器,但他们无法为我解决这个问题。

我有这两个功能。

public List<T> Find<T>(Func<T, bool> predicate) where T : class
    {
        var objectSet = db.CreateObjectSet<T>();
        return objectSet.Where<T>(predicate).ToList();
    }

    public void Delete<T>(Func<T, bool> predicate) where T : class
    {
        var objectSet = db.CreateObjectSet<T>();
        IEnumerable<T> records = from x in objectSet.Where<T>(predicate) select x;

        foreach (T record in records)
        {
            objectSet.DeleteObject(record);
        }
    }

现在转换器告诉我:

Public Function Find(Of T As Class)(predicate As Func(Of T, Boolean)) As List(Of T)
    Dim objectSet = db.CreateObjectSet(Of T)()
    Return objectSet.Where(Of T)(predicate).ToList()
End Function

Public Sub Delete(Of T As Class)(predicate As Func(Of T, Boolean))
    Dim objectSet = db.CreateObjectSet(Of T)()
    Dim records As IEnumerable(Of T) = From x In objectSet.Where(Of T)(predicate) Select x

    For Each record As T In records
        objectSet.DeleteObject(record)
    Next
End Sub

但我得到两个错误,两者objectSet.Where<T>(predicate)都是:

重载解析失败,因为没有可访问的“Where”接受此数量的类型参数。

4

1 回答 1

4

只需从对 的调用中删除 type 参数Where,无论如何都没有必要。其次,决定一种风格:要么使用 LINQ 要么使用函数,而不是两者兼而有之。

那是,

  • 要么写:

    Dim records As IEnumerable(Of T) = From x In objectSet Where predicate(x) Select x
    

    …但在 VB 中,您可以省略Select x

    Dim records As IEnumerable(Of T) = From x In objectSet Where predicate(x)
    
  • 或写:

    Dim records As IEnumerable(Of T) = objectSet.Where(predicate)
    

Find. 简单地写:

Return objectSet.Where(predicate).ToList()
于 2012-10-03T12:02:43.500 回答