I want to port some of my EntityFramework C# code into VB.NET. I’m trying different online code convertor but they can’t solve this problem for me.
I have these two functions.
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);
}
}
Now convertors tell me this:
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
But I get two errors, both for objectSet.Where<T>(predicate) which is:
Overload resolution failed because no accessible ‘Where’ accepts this number of type arguments.
Simply remove the type argument from your call to
Where, it’s unnecessary anyway. Secondly, decide on a style: either use LINQ or functions, not both.That is,
either write:
… but in VB you can leave off the
Select x:or write:
Same for
Find. Simply write: