dimanche 4 janvier 2015

How can I put a property to linq expression at runtime?

I need to pass property name to the "GetClientsByFilter()" method via string parameter "propertyName" and use this property in LINQ expression.


It is assumed that there is need to use reflection.


Do you have any idea what I have to use instead string that with comment "//pseudo code" below? Thanks.



public class Client
{
public string FirstName { get; set; }
public string LastName { get; set; }
}

internal class DataLayer
{
public static List<Client> GetClientsByFilter(string search, string propertyName)
{
//it's Entity Framework context
using (var dbContext = new LibDbContext())
{
List<Client> clients;

clients = dbContext.Clients
.Where(item => item.[propertyName].Contains(search)) // pseudo code
.ToList();
}
return clients;
}
}





Aucun commentaire:

Enregistrer un commentaire