Converting SQL inner join, group by and order to LINQ - sql-server

I have created an SQL query as follows:
SELECT s.FullName, s.[Id], COUNT(t.[User]) AS Records
FROM dbo.AspNetUsers s INNER JOIN dbo.PBBuilds t
ON s.Id = t.[User]
GROUP BY t.[User], s.[FullName], s.[Id]
ORDER BY COUNT(t.[User]) DESC
I'm attempting to convert this to a LINQ query, and this is how far i have got:
Dim results = From H In context.Users
Join C In context.PBBuilds On H.Id Equals C.User
Group By C.User Into
Could anyone please advise.

Try this query -- (make changes accordingly though)
var results = From U In context.AspNetUsers
Join P In context.PBBuilds On U.Id Equals P.User
Group By new { P.User, U.FullName } Into g
Orderby COUNT(P.User) Descending
select new {
FullName = U.FullName,
ID = U.Id,
Records = COUNT(P.User)
};

try code
var results = From U In context.AspNetUsers
Join P In context.PBBuilds On U.Id Equals P.User
Group new {U,P} By new { P.User, U.FullName } Into g
select new {
FullName = g.FirstOrDefault().U.FullName,
ID = g.FirstOrDefault().U.Id,
Records = g.Count()
}).OrderByDescending(c=>c.Records).ToList();

Related

How do you convert a subquery to work with entity framework?

In the past I would create a view for this but know there has to be a 'right way'.
Say I have this query:
SELECT
C.CustomerID
,C.ClientID
,C.Email
,C.PhoneNum
,C.PhotoCredits
,C.CreateDt
,C.Accessed
,C.Name
,C.Notes
,(SELECT COUNT(*) FROM PHOTOS P WHERE P.CUSTOMERID = C.CustomerID) as "COUNT"
,(SELECT COUNT(*) FROM PHOTOS P WHERE P.CUSTOMERID = C.CustomerID AND PURCHASEDT is not Null) as "PURCHASED"
FROM CUSTOMERS
In my controller, I currently have this:
public IQueryable<Customer> GetCustomers()
{
Guid clientID = Guid.Parse(User.Identity.GetUserId());
return db.Customers.Where(c => c.ClientID == clientID).AsQueryable();
}
Is there something I could add to my model to include these counts, or is there a way to add them in the controller so that they can be used in my view? Thanks!
Something like:
var q = from c in db.Customers
where c.ClientId = clientId
select new
{
c.CustomerID,
c.ClientId,
//. . .
Count = c.Photos.Count,
PurchasedCount = c.Photos.Where( p => p.PurchaseDate != null )
};

How convert this consult in LinQ

I'm trying to convert in this query
from lckr in BD.Inventory_Lockers join emp in BD.Employees on lckr.EmployeeID equals emp.EmployeeID
join jti in BD.JobTitles on emp.JobTitleID equals jti.JobTitleID
join dpt in BD.JobTitles on jti.DepartmentID equals dpt.DepartmentID
select new { DepartmentN = dpt.Department.DepartmentName, asignationDate= lckr.asignation_date, lockers = lckr.LockersID.Count() }
.ToList())
I understand that you want to convert to T-SQL, it would be as follows
SELECT
dpt.DepartmentName AS DepartmentN
,lckr.asignation_date AS asignationDate
,COUNT(lckr.LockersID) AS lockers
FROM Inventory_Lockers lckr
JOIN Employees emp ON emp.EmployeeID = lckr.EmployeeID
JOIN JobTitles jti ON jti.JobTitleID = emp.JobTitleID
JOIN Department dpt ON jti.DepartmentID = dpt.DepartmentID
GROUP BY
dpt.DepartmentName
,lckr.asignation_date

need to convert posted sql query to LINQ

How do i make that query in linq please advise
select *
from tblPermission
where RoleId in (select roleid from tbluserrole where userid = #userID)
When converting from SQL to LINQ, convert in the order of the LINQ phrases. If your SQL has table aliases, use them as the range variables. If a query contains a sub-query, translate it first in the same way. Translate IN into Contains.
var roles = from ur in tbluserrole where ur.userid == parmUserId select ur.RoleId;
var ans = from p in tblPermission
where roles.Contains(p.RoleId)
select p;
You can do this in 2 ways:
var roles = tbluserrole.Where(e => e.userid == userId).Select(e => e.roleid).ToList();
var prmsns = tblPermission.Where(e => roles.Contains(e.RoleId).ToList()
or
var prmsns = (from e in tblPermission
let roles = tbluserrole.Where(f => f.userid == userId).Select(f =>
f.roleid).ToList();
where roles.Contains(e.RoleId)
select e).ToList();
from
EDIT:
you can do this with inner join like this
var prmsns = (from e in tblPermission
join f in tbluserrole on tblPermission.RoleId equals tbluserrole.roleid
where f.userId == userId
select e).ToList();

How to convert sql query into Linq to entities?

I am new to joins in linq to entities. I have one query in sql server and want to convert it into LINQ to Entities.
Can someone please provide the solution for the same? Do we have any online tool to convert sql queries to LINQ to entities?
SELECT R.ID,r.Name,u.UserId
FROM Roles R
Left JOIN UserRoles U ON r.Id = u.RoleId
AND [UserId] = '5'
where [UserId] IS NULL
DefaultIfEmpty will result in a left outer join, therefore as you want a simple left join you should do as follows:
var list = (from r in context.Roles
join u in context.UsersRoles on r.Id equals u.RoleId && u.UserId='5' into x
where r.UserId == null
select new
{
r.Id,
r.Name,
u.UserId
}).ToList();
var list = (from r in context.Roles
join ur in context.UsersRoles on r.Id equals ur.RoleId && ur.UserId='5' into x
from y in x.DefaultIfEmpty()
where r.UserId ==null
select new Object
{
Id = r.Id,
Name = r.Name,
ur.UserId
}).ToList();
Note: Not understand your second UserId IS NULL logic

Sql query to Left Join in Linq to entity

I've below SQL query
Select LC.*,LP.[LandingPageName] from [LandingPageCompanies] LC
Left join [LandingPageContent] LP on LP.SubCategoryID=LC.SubCategoryID
where LC.[CategoryID]=17
And i need to convert it into LINQ to entity.
i've tried the following query, But it's working as Inner join
var data = (from t1 in dbSavingContainer.LandingPageCompanies
join t2 in dbSavingContainer.LandingPageContents on t1.SubCategoryID equals t2.SubCategoryID
where t1.CategoryID == CategoryID
select new
{
CategoryID = t1.CategoryID,
CompanyID = t1.CompanyID,
CompanyLink = t1.CompanyLink,
CompanyLogo = t1.CompanyLogo,
CompanyName = t1.CompanyName,
SubCategoryID = t1.SubCategoryID,
LandingPageName = t2.LandingPageName
}).ToList();
Where i'm lacking. ?
Thanks..i just solved it using below query ;)
var data = (from t1 in dbSavingContainer.LandingPageCompanies
join t2 in dbSavingContainer.LandingPageContents on t1.SubCategoryID equals t2.SubCategoryID
into x from y in x.DefaultIfEmpty()
where t1.CategoryID == CategoryID
select new
{
CategoryID = t1.CategoryID,
CompanyID = t1.CompanyID,
CompanyLink = t1.CompanyLink,
CompanyLogo = t1.CompanyLogo,
CompanyName = t1.CompanyName,
SubCategoryID = t1.SubCategoryID,
LandingPageName = y.LandingPageName
}).ToList();

Resources