something like this, I did not compile or test it, but it should give you the key.
public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate) { using(var db = new requestsDBEntities()) { DateTime dtfirstDate =null; DateTime.TryParse(firstDate,out dtfirstDate); DateTime dtlastDate = null; DateTime.TryParse(lastDate,out dtlastDate); var result = (from r in db.requests where (r.departmant == departmant) || (r.reqDate <= dtlastDate.Value && r.reqDate >= dtfirstDate.Value) select new ProductReqNoDate { departmant = r.departmant, reqNo = r.reqNo , reqDate = r.reqDate , prdctName= stringCutter((from p in db.products where p.reqNo == r.reqNo select p.prdctName).FirstOrDefault()) }).ToList(); } }
EDIT:
if you want to use non-sql functions in your filter then call ToList () in your table
var result = db.requests.ToList().Where(r => { // do test for what you want // so write a function to work out when you want to filter by // name or date return true; }).Select( r => new ProductReqNoDate { departmant = r.departmant, reqNo = r.reqNo , reqDate = r.reqDate , prdctName= stringCutter(db.products.Where(p=> p.reqNo == r.reqNo).Select(p=> p.prdctName).FirstOrDefault()) })
the problem is that you load the entire table into memory, which in case of a penalty, if you do not change the values ββin it often and if it is not too large.
Another approach would be to write it as a stored procedure, which is likely to make your DBA happy.
source share