I've been trying to get the following SQL
SELECT *
FROM dbo.VirtualMachines vm
WHERE vm.SequenceId IN (
SELECT MAX(SequenceId) FROM dbo.VirtualMachines GROUP BY RequestId
)
AND vm.DeletedBy IS NULL
...into a LINQ-query for use with NHibernate.
I've been able to get a variation on this working which is based on a corrolated sub-query:
var allvms = from vms in this.currentSession.Query<Entities.VirtualMachine>()
where vms.DeletedBy == null
where vms.Id == (
from activeVms in this.currentSession.Query<Entities.VirtualMachine>()
where activeVms.RequestId == vms.RequestId
orderby activeVms.Id descending
select activeVms.Id).First()
orderby vms.RequestId
select vms;
...which gives me...
SELECT *
FROM dbo.VirtualMachines vm
WHERE vm.SequenceId IN (
SELECT TOP 1 zvm.SequenceId From dbo.VirtualMachines zvm WHERE zvm.RequestId = vm.RequestId ORDER BY zvm.SequenceId DESC
)
AND vm.DeletedBy IS NULL
...however I'd rather use the MAX() version as (having profiled SQL Server) it's a more efficient query for the dataset I'm working with. Unfortunatly, I can't work out how to wrangle LINQ to give me the query.
I know I can do:
from vms in this.currentSession.Query<Entities.VirtualMachine>()
group vms by vms.RequestId into vmReqs
select new {
LatestSeqId = vmReqs.Max(vms => vms.SequenceId)
}
which gives me the sub-select (SELECT MAX(SequenceId) [...]), but I can't see how to combine this with the query I've already got to do an IN. It's possible I'm approaching this in too-SQL a mindset, and I'm trying to approach the query as I would in SQL and there's some other technique I've missed.
Something like this:
var subQuer = from vms in this.currentSession.Query<Entities.VirtualMachine>()
group vms by vms.RequestId into vmReqs
select vmReqs.Max(vms => vms.SequenceId);
var outerQuery = from vm in this.currentSession.Query<Entities.VirtualMachine>()
where subQuery.Contains(vm.SequenceId) && vm.DeletedBy == null;
Related
I am trying to write a LINQ query equivalent to below SQL
SELECT DISTINCT m.*,rm.RoleId FROM dbo.Menu m
INNER JOIN dbo.RoleMenu rm on m.Id=rm.MenuId
INNER JOIN dbo.RoleUser ru on rm.RoleId=ru.RoleId
WHERE ru.UserName='dd#dd.com' and m.Url='/dashboard#/pm'
I came with the below query which is not returning the expected output
var auth = _context.RoleUsers.Where(
x => x.Role.MenuRoles.FirstOrDefault().Menu.Url == pagePermissions.Url
&& x.UserName == pagePermissions.UserName).Count()
May I know a better way to do this?
Your sql looks at all the menus related to a role user, but your Linq is only looking at the first one. I think you want x.Role.MenuRoles.Any(mr => mr.Menu.Url == pagePermissions.Url). But then you're also doing a Count on the matching users instead of selecting the menus that match that url. A closer translation would be.
var results = (from m in _context.Menus
from rm in m.RoleMenus
from ru in rm.RoleUsers
where m.Url == pagePermissions.Url
&& u.UserName == pagePermissions.UserName
select new { Menu = m, rm.RoleId }).Distinct();
You may have to adjust some of the navigation properties as I was just guessing at them. They usually are pluralizations of the tables, but I see in your Linq that you have MenuRoles instead of RoleMenus.
How can I rewrite the following Linq query into normal T-SQL?
var list = (from t in context.ParentTable
where t.ChildRecords.Count == t.ChildRecord.Count( c => c.BooleanColumn )
select t).ToList();
Thanks in advance...
Something like this, but you'll need to determine the relationship between the ParentTable and ChildRecord tables to make it work, I'm just guessing at the cr.ParentTableId = pt.ParentTableId part.
select pt.*
from ParentTable pt
where not exists
(select 1
from ChildRecord cr
where cr.ParentTableId = pt.ParentTableId
and cr.BooleanColumn = 0)
On a side note the Linq could be changed to the following instead.
var list = (from t in context.ParentTable
where t.ChildRecords.All(c => c.BooleanColumn)
select t).ToList();
Hierarchical Structures Database Select By Lambda Expression
Hi My Table Structure is recursive
ID
ParentID
UserGROUPNAME
How Can I select Subset an Id from my contex By Lambda Expression like this
I Try to select first
var keys = db.UsersGroups.Select(x => x).Where(u => u.GroupMasterID == 1).ToArray();
var UsersGroup = db.UsersGroups.Where(x => keys.Contains(x.GroupMasterID));
But I think it's return wrong or return just 2 level data
Please Help me
That´s something that you cannot do, without mess it up, with linq. What you have to do is to flatten the hierarchy using a probably a common table expression and then you will be able to write a simple linq query.
finally I create a sql function
like this
Create FUNCTION [dbo].[GET_USERGROUPLIST]
(
#GROUPID INT
)
RETURNS TABLE
AS
RETURN
(
WITH UsersGroups_CTE AS (
SELECT *
FROM [dbo].[UsersGroups]
WHERE ID = #GROUPID
UNION ALL
SELECT e.ID, e.GroupName, e.ParentID ,e.Status
FROM [dbo].[UsersGroups] e
INNER JOIN UsersGroups_CTE ucte ON ucte.ID = e.ParentID
)
SELECT *
FROM UsersGroups_CTE Where UsersGroups_CTE.Status = 1
);
and Next Use in my model like this
db.GET_USERGROUPLIST(1);
Many Thanks to lontivero for him suggestion
I want to filter my LINQ query based on an included table but am having some trouble.
Here is the original statement, which works:
return
this.ObjectContext.People.
Include("Careers").
Include("Careers.Titles").
Include("Careers.Titles.Salaries");
Now I'm trying to filter on Careers using projected filtering but am having trouble. It compiles but it leaves out the Titles and Salaries tables, which causes runtime errors, and I can't seem to add those tables back in:
var query1 = (
from c in
this.ObjectContext.People.
Include("Careers").
Include("Careers.Titles").
Include("Careers.Titles.Salaries")
select new
{
c,
Careers = from Careers in c.Careers
where Careers.IsActive == true
select Careers
});
var query = query1.AsEnumerable().Select(m => m.c);
return query.AsQueryable();
How can I include the titles and salaries tables in the filtered query?
You can simplify your query considerably, which should resolve your issue. I'm assuming that you want all people with at least 1 active career:
var query =
from c in
this.ObjectContext.People.
Include("Careers").
Include("Careers.Titles").
Include("Careers.Titles.Salaries")
where c.Careers.Any(c => c.IsActive);
return query;
I would try something like,
var query = from p in ObjectContext.People
join c in ObjectContext.Careers on p equals c.Person
where c.IsActive
select p;
Have the following (non-straightforward) T-SQL query, which i'm trying to convert to LINQ (to be used in a L2SQL expression):
declare #IdAddress int = 481887
select * from
(
select top 3 p.*
from tblProCon p
inner join vwAddressExpanded a
on p.IdPrimaryCity = a.IdPrimaryCity
where a.AddressType = 3
and p.IsPro = 1
and a.IdAddress = #IdAddress
order by AgreeCount desc
) as Pros
union
select * from
(
select top 3 p.*
from tblProCon p
inner join vwAddressExpanded a
on p.IdPrimaryCity = a.IdPrimaryCity
where a.AddressType = 3
and p.IsPro = 0
and a.IdAddress = #IdAddress
order by AgreeCount desc
) as Cons
order by ispro desc, AgreeCount desc
In a nutshell, i have an #IdAddress - and i'm trying to find the top 3 pro's and top 3 con's for that address.
The above query does work as expected. I'm not entirely sure how to convert it to a LINQ query (never done unions before with LINQ). I don't even know where to start. :)
Query-style/Lambda accepted (prefer query-style, for readability).
Also - i have LinqPad installed - but i'm not sure how to "convert T-SQL to Linq" - is there an option for that? Bonus upvote will be awarded for that. :)
The above T-SQL query performs well, and this L2SQL query will be executed frequently, so it needs to perform pretty well.
Appreciate the help.
var baseQuery = (from p in db.tblProCon
join a in db.vwAddresssExpanded
on p.IdPrimaryCity equals a.IdPrimaryCity
where a.AddressType == (byte) AddressType.PrimaryCity &&
a.IdAddress == idAddress
order by p.AgreeCount descending
select p);
var pros = baseQuery.Where(x=> x.IsPro).Take(3);
var cons = baseQuery.Where(x=> !x.IsPro).Take(3);
var results = pros
.Union(cons)
.OrderByDescending(x => x.IsPro)
.ThenByDescending(x => x.AgreeCount)
.ToList();
You can call (some query expression).Union(other query expression).
You can also (equivalently) write Enumerable.Union(some query expression, other query expression).
Note that both expressions must return the same type.
AFAIK, there are no tools that automatically convert SQL to LINQ.
(For non-trivial SQL, that's a non-trivial task)