Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Linq Query Dynamically

I'm writing LINQ query that implements a keyword-style search. In other words, a query that returns rows whose Title,Description,Id contains

As follows

    public static IQueryable<EmployeeObject> QueryableSQL()
    {
        IQueryable<EmployeeObject> queryable = EmployeeRepository.GetAllEmployee(); 
    }

    public static IList<EmployeeObject> QLike(string txt)
    {
       IList<employeeObject> _emps = QueryableSQL().Where(x => x.Title == txt).ToList();
       return _emps;
    }

So far, so good. But this only handles the case where you want to match in my case Title.

Suppose instead, I wanted to search based on the 'DescriptionorIdshould i have to create a new method for Description? or is there a way i can create aTree expression`

like image 872
Nick Kahn Avatar asked Sep 13 '26 19:09

Nick Kahn


1 Answers

public static IQueryable<EmployeeObject> QueryableSQL()
{
    IQueryable<MediaObject> queryable = EmployeeRepository.GetAllEmployee(); 
}

public static IList<EmployeeObject> QLike(Expression<Func<EmployeeObject, bool>> func)
{
   return QueryableSQL().Where(func).ToList();
}

You can then call this like:

QLike(t => t.Title == "MyText");
like image 113
gleng Avatar answered Sep 17 '26 17:09

gleng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!