SQL to LINQ with outer joins and count - sql

I am trying to write the LINQ statement for the following OUTER JOIN with COUNT but can't seem to work it out..
My LINQ skills aren't what they should be yet so any pointer would be greatly appreciated.
The SQL statement in question is:
SELECT b.Id,
b.Text,
b.Active,
COUNT(u.BusinessArea_Id)
FROM dbo.[User] AS u RIGHT OUTER JOIN dbo.BusinessArea AS b ON b.Id = u.BusinessArea_Id
GROUP BY b.Id, b.Text, Active
ORDER BY b.Id

I think you can use a linq like this:
var res = (from ba in businessAreas
let count = users.Count(u => u.BusinessArea_Id == ba.Id)
orderby ba.Id
select new {ba.Id, ba.Text, ba.Active, Count = count}
).ToList();

Related

How to convert left join sql query to linq to sql

I have following query in SQL. I want the same operation from linq to sql.
select count(bs.sampleId),s.sampleCode from prod.Samples s
LEFT join prod.BlockedSamples bs on bs.sampleId = s.sampleId
group by s.sampleCode
having count(bs.sampleId)>0
Relation between these two tables is 1 to many. 1 sample can have multiple entries in Blocked Sample.
What about
from s in context.Samples
join bs in context.BlockedSamples on s.sampleId
equals bs.sampleId into ps
from p in ps.DefaultIfEmpty()
group p by s.sampleCode into grouped where grouped.Count(t=>t.sampleId != null)>0
select new {key=grouped.Key,Count =
grouped.Count(t=>t.sampleId != null)}
For this and future efforts of converting SQL to LINQ you can use:
sqltolinq

Convert SQL query to Linq query with left outer join and where clause

I want to convert this SQL query to a Linq query
select
prsnRolIDTitle, prprdID
from
Tbl_RoleDetail
left outer join
Tbl_PersonRolPersonRolDetail on Tbl_RoleDetail.prsnRolDID = Tbl_PersonRolPersonRolDetail.prsnRolDID
and Tbl_PersonRolPersonRolDetail.prsnRolID = 6
Thanks for helping!
Maybe something like this:
from rd in Tbl_RoleDetail
from Tbl_PersonRolPersonRolDetail.Where(t => t.prsnRolID = 6 && t.prsnRolDID == rd.prsnRolDID).DefaultIfEmpty()
select new {rd.prsnRolIDTitle, prd.prprdID}

Converting nested SQL statement to LINQ

I need help to figure out how the following sql statement can be converted into a linq statement
SELECT distinct tableA.x, tableA.y, tableA.z
FROM tableA inner join
tableB on tableA.id = tableB.id inner join
tableC on tableA.id = tableC.id
WHERE (tableB.columnOne IN (SELECT tableX.columnOne
FROM tableX INNER JOIN
tableY ON tableX.xId = tableY.xId
WHERE (tableY.xId = tableC.xId) )
AND (tableB.columnTwo IN (SELECT tableXx.columnTwo
FROM tableXx INNER JOIN
tableYy ON tableXx.XxId = tableYy.XxId
WHERE tableYy.XxId =tableC.XxId)))
)
I suppose the first thing to point out is that you can do sub queries in LINQ. Within your main Where clause, you could probably create queries for your subqueries, (which would result in IQueryable types) and then use .Any() to predict if there are any matches e.g.
var tableXquery = {my tableX subquery};
var tableXxquery = {my tableXx subquery};
var result = context.tableB.where(b => tableXquery.any(x => x.columnOne == b.columnOne) && xx => tableXxquery.any(xx => xx.columnTwo == b.columnTwo));
I hope you get the idea - I just tend to put queries where they need to be, and let LINQ sort it out!

LINQ with Conditional Left Join

Hi there i am a newbie in linq,
i tried to do a left join query using LinQ. This is my query:
SELECT B.[ID]
,A.[GroupID]
,B.[Year]
,B.[Month]
,B.[Col1]
,B.[Col2]
,B.[Col3]
,B.[Col4]
,B.[Col5] FROM MsGroups A
LEFT JOIN MsCKPNS B on
A.GroupId = B.GroupId and B.Year = 2014 and B.Month = 3
Here is my linq query:
Dim msckpn = (From g In db.MsGroup
Group Join c In db.MsCKPNs
On g.GroupId Equals c.GroupID Into
Group From a In Group.DefaultIfEmpty() _
Where a.Month = Month And a.Year = Year
Select New MsCKPN With {
.ID = Nothing,
.GroupID = g.GroupId,
.Month = Month,
.Year = Year,
.Col1 = a.Col1,
.Col2 = a.Col2,
.Col3 = a.Col3,
.Col4 = a.Col4,
.Col5 = a.Col5
}).ToList
But, it shows error : The entity or complex type 'MvcMISD.MsCKPN' cannot be constructed in a LINQ to Entities query.
What should i do to fix the query?
Thanks
You can't create an entity like this. The reason is that you are not supposed to be able to create an entity which isn't linked to a record or records in the database. An explanation of this is given in the comments below this answer: https://stackoverflow.com/a/5325861/1737957 You can create an anonymous type which has the fields you want.
Some other parts of your code look a little strange but I'm not so familiar with LINQ in VB so I could be misunderstanding your query.

Convert LINQ query to Sqlquery

I need help to convert this query from LINQ syntax into SQLServer query syntax:
var t1 = (from p in db.Varors join
op in db.OrderVarors on p.id equals op.IdVara
where op.IdOrder == OrderId
select p).ToList();
Hope this helps:-
SELECT v.* from Varors v
JOIN ordervarors ov on v.id = ov.id
where ov.idOrder == #OrderId
You may want to choose individual columns instead of star but this should work.
select *
from dbo.Varors as P
join dbo.OrderVarors as OP
on p.id equals op.IdVara
where op.IdOrder = OrderId
this is a simple join - try this :
select Varors.*
from Varors
join OrderVarors on Varors.id = OrderVarors.IdVara
where OrderVarors.IdOrder = #OrderId