Hi
I am trying to build a SQL query that works 100% in SQL server in stored procedure for fetching records on dynamic criteria like it will filter the record if parameter value is given, if left Null it will not include it in criteria and will fetch all records like:
DECLARE @LocationId int;DECLARE @IsPaid bit;SET @IsPaid = NULL;--SET @LocationId = 1;SELECT * FROM [TicketInfo]WHERE ((@LocationID ISNOTNULLAND [LocationId] = @LocationID) OR @LocationId ISNULL) AND ((@IsPaid ISNOTNULLAND [IsPaid] = @IsPaid) OR @IsPaid ISNULL)
so if I SET @IsPaid = 1; it will fetch those records which are paid, if I also include SET @LocationId = 1 then it will get "Paid record with location equals 1"
Problem: I want to make this query in LINQ but that doesn't seem to be working
return dc.TicketInfos.Where(t => ((locationId != null&& t.LocationId == locationId) || t.LocationId == null) && ((issuerId != null&& t.IssuerId == issuerId) || t.IssuerId == null) && ((isPaid != null&& t.IsPaid == isPaid) || t.IsPaid == null)).ToList();