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();
Related
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();
I having problems converting linq query to a SQL Server query.
var gdevices = (from logs in dbContext.GensetLogs
group logs by logs.DeviceId into logsgroup
join devices in dbContext.GensetDevices on logsgroup.FirstOrDefault().DeviceId equals devices.Id
where devices.RegisteredBy == model.Email || devices.OperatedBy == model.Email || model.StType == "admin"
select new DeviceRegistrationDTO
{
PhoneNumber = devices.PhoneNumber,
Latitude = devices.Latitude,
Longitude = devices.Longitude,
LatestRT = logsgroup.Max(d => d.ReadingTime),
DeviceName = logsgroup.Max(d => d.ReadingTime).DeviceName,
OperatedBy = devices.OperatedBy,
ThresholdValue = devices.ThresholdValue
}).ToList();
If you are trying to convert from Linq to sql queries then use as following
var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();
Ref:
https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx
i done this in very simple way.
SELECT (t.[TransId])
, t.[SGCode]
, t.PurchaseDate
, t.SoldTo
,t.Cost
,(select top 1 dr.Rate from DepreciationRate dr where t.Assets_TransId = dr.Assets_TransId order by dr.DepDate desc) Rate
,(select sc.Name from AssetsSubClass sc where t.SubClass_TransId = sc.TransId) Name
FROM AssetsTransctions t
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
I'm having some trouble 'converting' a T-SQL query to linq using Entity Framework v6.
My working T-SQL query looks like this:
SELECT
[Person].*, [Person_Firm_PersonResponsibility].*
FROM
[Person]
JOIN
[Person_Firm_PersonResponsibility] ON [Person].ID = [Person_Firm_PersonResponsibility].PersonID
WHERE
[Person_Firm_PersonResponsibility].FirmID = 389
AND [Person].ID = 330
In Linq I have the following:
using (var ctx = new MyContext())
{
var result = (from p in ctx.People
join r in ctx.Person_Firm_PersonResponsibility on p.ID equals r.PersonID
where r.FirmID == firmId && p.ID == personId
select p)
.Include("Person_Firm_PersonResponsibility.PersonResponsibility")
.Include("Person_Firm_PersonResponsibility")
.FirstOrDefault();
return result;
}
My goal is to only get a specific Person with responsibilities for a specific firm (firm ID) The relation between Firm, Person and Responsibilities is the many-to-many Person_Firm_PersonResponsibility table which I'm querying against.
The query 'works' in the sense that I get a the person, but it includes all of his/her responsibilities for any firm and not the specific firm (firmId) I've tried removing the .Include(""), but that didn't do it.
Does anyone know how to achieve this?
Try this:
from p in Persons
join r in Person_Firm_PersonResponsibilities
on p.ID equals r.PersonId
where (r.FirmId == 389 && p.ID == 330)
select new {p, r}
Tested using LinqPad which shows the equivalent SQL as being:
-- Region Parameters
DECLARE #p0 Int = 389
DECLARE #p1 Int = 330
-- EndRegion
SELECT [t0].[ID], [t0].[Name], [t1].[FirmId], [t1].[PersonId]
FROM [Person] AS [t0]
INNER JOIN [Person_Firm_PersonResponsibility] AS [t1] ON ([t0].[ID]) = [t1].[PersonId]
WHERE ([t1].[FirmId] = #p0) AND ([t0].[ID] = #p1)
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();