Thursday 12 July 2012

Use Dynamic field in Linq WHERE clause

Sometimes you need to do something like :

SELECT * FROM table WHERE sFieldName = sValue

Here is how to translate the above to LINQ

Dim db As New northwindDataContext    
Dim sFieldName"City"
Dim sFieldValue"Berlin" 
Dim q = From t In db.Customers.AsEnumerable.
        Where(Function(ff.GetType.GetProperty(sFieldName).GetValue(f,Nothing)  = sFieldValue)
        Select t
 



No comments:

Post a Comment