Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm able to convert the PK line but not the following in my VS 2010 project. I've looked at expression trees, lambdas, etc. but haven't gotten anything to work yet. The automated tools I've found online don't convert it correctly. Any help would be appreciated.

C#
public virtual void ReadRecordFromDB<T>(Int32 ID) where T : class
{

...
C#
// find the primary key field name by checking for IsPrimaryKey
string pk = (dataMembers.Single<MetaDataMember>(m => m.IsPrimaryKey)).Name;

// return a single object where the id argument matches the primary key field value
var tbl = table.SingleOrDefault<T>(delegate(T t)
{
    String memberId = t.GetType().GetProperty(pk).GetValue(t, null).ToString();
    return memberId.ToString() == ID.ToString();
});
Posted

Something like this should work:
VB.NET
Public Overridable Sub ReadRecordFromDb(Of T As Class)(ByVal ID As Integer)
    ...
    
    ' find the primary key field name by checking for IsPrimaryKey
    Dim pk As String = dataMembers.Single(Function (m) m.IsPrimaryKey).Name
    
    ' return a single object where the id argument matches the primary key field value
    Dim tbl As T = table.SingleOrDefault(Function (t2)
        Dim memberId As String = t2.GetType().GetProperty(pk).GetValue(t, Nothing).ToString()
        Return memberId == ID.ToString()
    End Function)
    
    ...
End Sub
   
v2
This solution looked promising but after I changed the == to a single =, then I got the error
"'t' is already declared as a type parameter of this method."
The t inside the Function(t) is highlighted as the error.

if I change the t to another letter then I get the error
"Statement lambdas cannot be converted to expression trees"

I'm still missing something simple with the syntax, I think.
   
Comments
Richard Deeming 11-Jun-14 7:42am
   
Please don't post comments as new solutions; use the "Have a Question or Comment?" button under the solution.

VB is case-insensitive, so the lambda function's parameter cannot have the same name as the main function's generic type parameter. I've updated my solution.
I gave up and kept it in C# in a separate project and called it from a VB project
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900