Last post Jun 24, 2016 05:25 PM by PatriceSc
Jun 24, 2016 03:06 PM|RDowdall|LINK
Hello - I have linq query that I need to modify based on if a couple of Pass / Fail check boxes are checked. I have an object called cp with a property named status. If pass only is checked cp.status = "pass", if fail only is checked cp.status = "fail",
if both are checked or both unchecked = cp.status="both". Below is my linq query, I can I integrate a if statement into the query based on cp.status?
public static IEnumerable<oCAVPC> Search(oCAVPC cp)
CAVPCDataContext db = new CAVPCDataContext();
string Person = cp.Operator.Trim();
string Product = cp.Product.Trim();
string Serial = cp.Serial.Trim();
string status = cp.Status; //Possible "Pass","Fail","Both"
IEnumerable<oCAVPC> result = (from p in db.CAVPC_manuals
where (p.CreationDate >= cp.DateFrom && p.CreationDate <= cp.DateTo)
&& p.CustomerNumber == Serial
&& p.Status == status //here is where I need to handle both pass and fail.
select new oCAVPC
Product = p.ProductName,
Reference = p.ReferenceName,
Serial = p.CustomerNumber,
Operator = p.OperatorName,
POSO = p.POSONumber,
Status = p.Status,
CreateDate = Convert.ToDateTime(p.CreationDate)
Jun 24, 2016 05:25 PM|PatriceSc|LINK
Should work IMO. Have you tried something? Another approach is that Linq queries are "composable" so you can create a query, and "enrich" it before actually running it ie :
var q=<base query>;
You can do that as well with other criterias so that finally just the criteria you really need are applied. It can be simpler IMO to construct the query this way rather than to use a single huge "catch all cases" expression.