LINQ equivalent of my SQL Query for UNION ALL - sql-server

(SELECT * FROM SheetHay WHERE SheetStatus = 2)
UNION ALL(SELECT * FROM SheetHay WHERE SheetStatus = 1)
UNION ALL (SELECT * FROM SheetHay WHERE SheetStatus = 0)
UNION ALL(SELECT * FROM SheetHay WHERE SheetStatus= 3)
I get result set like this:
I mean I get all '2' together, '0' together, '3' together ( no '1' in
table yet). But when I use LINQ and UNION them I see the result on my grid in order of SheetID, the primary key. I mean I see the order of sheets displayed as 15,23,25,27,28,29 etc. But I want it just as the SQL result set.23,43,25,28 etc
IEnumerable<SheetHay> listTwos = Get(LINQ QUERY TO GET just twos);
IEnumerable<SheetHay> listOnes = Get(LINQ QUERY TO GET just Ones);
IEnumerable<SheetHay> listZeros = Get(LINQ QUERY TO GET just Zeros);
IEnumerable<SheetHay> listThrees = Get(LINQ QUERY TO GET just Threes);
....
return listTwos.Union(listZeros).Union(listOnes).Union(listThrees);
Let me know if you need any other information. Thanks.

You don't need to use multiple queries you can use CASE in the ORDER BY in sql and a similar way in LINQ.
SQL:
SELECT * FROM SheetHay
WHERE SheetStatus IN(0,1,2,3))
ORDER BY CASE SheetStatus
WHEN 2 THEN 1
WHEN 1 THEN 2
WHEN 0 THEN 3
WHEN 3 THEN 4 END ASC, SheetStatus ASC
LINQ:
int[] status = {0, 1, 2, 3};
var query = db.SheetHay
.Where(s => status.Contains(s.SheetStatus))
.OrderByDescending(s => s.SheetStatus == 2)
.ThenByDescending(s => s.SheetStatus == 1)
.ThenByDescending(s => s.SheetStatus == 0)
.ThenByDescending(s => s.SheetStatus == 3)
.ThenBy(s => s.SheetStatus);
Descending because a comparison returns bool and true is "higher" than false(1/0).
You could also use a conditional operator to return an int for the ordering:
var query = db.SheetHay
.Where(s => status.Contains(s.SheetStatus))
.OrderBy(s => s.SheetStatus == 2 ? 0 : 1)
.ThenBy(s => s.SheetStatus == 1 ? 0 : 1)
.ThenBy(s => s.SheetStatus == 0 ? 0 : 1)
.ThenBy(s => s.SheetStatus == 3 ? 0 : 1)
.ThenBy(s => s.SheetStatus);

What about this:
IEnumerable<SheetHay> sheetHays =
SheetHays
.Where(x => x.SheetStatus < 3)
.OrderByDescending(x => x.SheetStatus)
.Concat(SheetsHays.Where(x => x.SheetStatus == 3));
This is based on order 2, 1, 0, 3 (which is what you example query demonstrates). This will need to be modified if you want order 2, 0, 1, 3 (which is what you sample code implies).

Related

EF Core 3.1 produces incorrect query translation

I have the following EF Core 3.1 statement:
var orders = await _dbContext.Orders.Include(o => o.OrderReceivers)
.Where(o => o.BooleanFlag1 && o.OrderState == OrderState.SomeState && o.OrderReceivers.Any(o => o.BooleanFlag2))
.ToListAsync(cancellationToken);
and this is the generated SQL Server query:
SELECT /*All column names here*/
FROM [Schema].[Orders] AS [o]
LEFT JOIN [Schema].[OrderReceivers] AS [o0] ON [o].[Id] = [o0].[OrderId]
WHERE (([o].[ShouldSendBlackList] = CAST(1 AS bit)) AND ([o].[OrderState] = 2)) AND EXISTS (
SELECT 1
FROM [Schema].[OrderReceivers] AS [o1]
WHERE ([o].[Id] = [o1].[OrderId]) AND ([o1].[BooleanFlag2] = '1'))
ORDER BY [o].[Id], [o0].[Id]
The problem with the generated SQL Query is this part:
([o1].[BooleanFlag2] = '1')
Because this causes the second where clause to be always false!
Any ideas on what is wrong here? Thanks in advance.

Need Equivalent LINQ Query for the following SQL Server Query

I am looking for the equivalent LINQ query for the following Query.
SELECT SUM (Cost) FROM [Vulpith_Test2].[dbo].tbl_MilestonesForOngoingSeeker]
WHERE ([Post_Title_ID] = '3251'
and [List_Title_ID]='1180')
and (PaymentStatus='1' or PaymentStatus='3');
int sumofCost = dbContext.tbl_MilestonesForOngoingSeeker.Where(a => a.Post_Title_ID == "3251" && a.List_Title_ID == "1180" && (a.PaymentStatus == 1 || a.PaymentStatus == 3)).Sum(a => a.Cost);

How to create drop-down list in SSRS based on WHERE clause below?

At my organization we using UI third party tool that communicates with SQL database. I have access to a database, but not to the tool.
Based on a stored procedure below user able to choose from drop-down list DairyStatus "Open", "Closed", or "Both"
ALTER Procedure
AS
#ShowOpen bit = 0,
#ShowClosed bit = 0
SELECT
FROM
WHERE
AND
(
(CASE WHEN (#ShowOpen = 1) THEN
CASE WHEN (tblNoteRecipients.CompletedDate IS NULL and tblNoteRecipients.IsDiary = 1) or tblNoteRecipients.UserGUID is null THEN 1 ELSE 0 END
ELSE
1
END = 1)
AND
(CASE WHEN (#ShowClosed = 1) THEN
CASE WHEN (tblNoteRecipients.CompletedDate IS NULL) THEN 0 ELSE 1 END
ELSE
1
END = 1)
OR ((#ShowOpen = 1) AND (#ShowClosed = 1))
)
So my question is how can I make same drop-down list in SSRS?
What would be the data-set in order to populate this drop-down list?
create a parameter in ssrs with 3 static values(open closed both)
in where clause it should be something like :
( #DairyStatus = 'open' and ((tblNoteRecipients.CompletedDate IS NULL and tblNoteRecipients.IsDiary = 1) or tblNoteRecipients.UserGUID is null)) or
( #DairyStatus = 'closed' and tblNoteRecipients.CompletedDate IS not NULL) or
#DairyStatus = 'both'

Linq query count

select count(tblVV.VNme) as total,
tblvV.VNme
from tblVV
inner join tblRV
on tblVV.MID=tblRV.ID
inner join tblRe
on tblRV.RID=tblRe.RID
where tblRe.StartDate>= '2016-07-01 00:00:00' and
tblRe.EndDate<= '2016-07-31 23:59:59' and
tblRe.Reg= 'uk' and
tblRV.RegNo='BR72' and
tblVV.VNme <>''
group by tblVV.VNme
For the above query I get:
total Vame
1 DDSB
11 MV
The above SQL query shows me correct data so now i try to convert above query to linq query
[WebMethod]
public static string GetVo(string RegNo)
{
string data = "[";
try
{
Ts1 DB = new Ts1();
var re = (from vehvoila in DB.tblVV
join regveh in DB.tblRV on vehvoila.MID equals regveh.ID
join reg in DB.tblReg on regveh.RID equals reg.RID
where regveh.RegNo == RegNo &&
vehvoila.Vame != ""
group vehvoila by vehvoila.Vame into g
select new
{
VNme = g.Key,
cnt = g.Select(t => t.Vame).Count()
}).ToList();
if (re.Any())
{
data += re.ToList().Select(x => "['" + x.Vame + "'," + x.cnt + "]")
.Aggregate((a, b) => a + "," + b);
}
data += "]";
}
linq query show me return data like this
[['DDSB',1],['DPSB',1],['DSB',109],['MV',39],['PSB',1]]
Whereas I want data this
[['DDSB',1],['MV',11]]
Now the data which return SQL query is correct so how I correct linq query
Note: forget fromdate,todate,region parameter in SQL query . because I have page in which I put dropdown and fromdate and todate picker and there is button so when I select values i.e. UK, and dates then data is display in table then when I click on any row in table then I want to get this data in data +=”]”;
actually above linq query work behind clicking on row
total Vame
1 DDSB
11 MV
You can write it all like this:
Ts1 db = new Ts1();
var result = (from vehvoila in db.tblVV
join regveh in db.tblRV on vehvoila.MID equals regveh.ID
join reg in db.tblReg on regveh.RID equals reg.RID
where reg.StartDate >= new DateTime(2016, 7, 1) &&
reg.EndDate < new DateTime(2016, 8, 1) &&
reg.Reg == "uk" &&
regveh == "BR72" &&
vehvoila != ""
group vehvoila by vehvoila.Vame into g
select $"[{g.Key},{g.Count()}]");
var data = $"[{string.Join(",", result)}]";
Because you only use the result for the creation of the string in the select I just return the string formatted for a single item and then later used string.Join instead of using the .Aggregate - I think a bit cleaner
The $"{}" syntax is the C# 6.0 string interpolation
In the condition of the EndDate I decided to use < instead of the <= with the change of the date - At least in oracle when you partition the table by date it is better for performance - maybe also in sql server
Without string interpolation:
Ts1 db = new Ts1();
var result = (from vehvoila in db.tblVV
join regveh in db.tblRV on vehvoila.MID equals regveh.ID
join reg in db.tblReg on regveh.RID equals reg.RID
where reg.StartDate >= new DateTime(2016, 7, 1) &&
reg.EndDate < new DateTime(2016, 8, 1) &&
reg.Reg == "uk" &&
regveh == "BR72" &&
vehvoila != ""
group vehvoila by vehvoila.Vame into g
select new { Key = g.Key, Count = g.Count()})
.AsEnumerable()
.Select(g => string.Format("[{0},{1}]",g.Key, g.Count));
var data = string.Format("[{0}]",string.Join(",", result));

how can I have different where condition using case statement?

#HistoryMSBType => This is a variable which can contain any varchar.
Depending upon it's value, i need to have different type of where clause.
How can I achieve this ?
SELECT * FROM stageTable map
WHERE id = 1
CASE #HistoryMSBType
WHEN 'Utilization Other' THEN
AND map.ColumnID = 4
WHEN 'Cost Other' THEN
AND map.ColumnID = 6
ELSE
AND map.ColumnName = #HistoryMSBType
END
SELECT *
FROM stageTable map
WHERE id = 1
AND (
(#HistoryMSBType = 'Utilization Other' AND map.ColumnID = 4)
OR
(#HistoryMSBType = 'Cost Other' AND map.ColumnID = 6)
OR
(isnull(#HistoryMSBType,'') NOT IN ('Utilization Other','Cost Other')
AND map.ColumnName = #HistoryMSBType)
)
You need the ISNULL to make it match the CASE-ELSE exactly, but it won't matter if it can never be null.

Resources