Linked Questions

Popular Questions

Generate a dynamic query accepting any Entity

Asked by At

I'm trying to create a query with LINQ, and then I want to assign the query generated to an entity class. For example I have three entities with the same columns.

  • A | Name, Lastname
  • B | Name, Lastname
  • C | Name, Lastname

For example I can generate a Linq Where Closures doing first a var who contains the select of the entity.

var lQuery = A.Select(t => t);

Then I add the closures if the Name or the LastName is not null

if (!string.IsNullOrEmpty(pName))
    lQuery = lQuery.Where(x => x.Name == pName);                    
if (!string.IsNullOrEmpty(pLastName))
    lQuery = lQuery.Where(x => x.LastName == pLastName);

And finally I return a list of A from the generated lQuery.

Is there a way to generate a query and then assign it at the end to one of my entities?

Related Questions