Quantcast
Channel: SQL Query in LINQ
Viewing all articles
Browse latest Browse all 7

SQL Query in LINQ

$
0
0

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();
I have also trimmed this query to one condition "isPaid" but that also isn't working. Any help in this regard will be highly appreciated

 


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images