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
Related
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();
I am trying to convert following SQL to QueryOver:
Select 1
From myTable mt
Where mt.ForeignKey in (select ID from otherTable ot where ot.ID = R.ID)
I want to use this subquery inside an EXISTS / NOT EXISTS statement like:
select * from table R where .... AND EXISTS (query above)
Currently I have something like:
mainQuery.WithSubquery.WhereExists(QueryOver.Of<myTable>()
.Where(mt => mt.ForeignKey)
.WithSubquery.IsIn(QueryOver.Of<otherTable>().Where(c => c.Id == R.SomeId)));
I created this query as a subquery which I want to connect to the main query.
The problem is that the table aliased as R is the table called by the main query and I don´t know how to access columns of the table (NHibernate Model) R (which is not accesible in the query above), so my question is:
How can I get values from the main query and use them in a subquery. I think this is only possible by creating the subquery inline (as in mainQuery.WithSubquery.Where(..) or smth. similar) but I can´t see what would be the best possible way to do so. I appreciate any help!
Thanks in advance!
The trick is to use proper alias, for the parent query:
// the alias
myTable R = null;
mainQuery
.WithSubquery
.WhereExists(QueryOver
.Of<myTable>( () => R) // the Alias in place
.Where(mt => mt.ForeignKey)
.WithSubquery.IsIn(QueryOver.Of<otherTable>().Where(c => c.Id == R.SomeId)));
Note, not fully sure about the mainQuery part, but the solution in general here is like this:
// I. the outer query ALIAS
Employee emplyoee = null;
// II. the subquery - using the alias
var subQuery = QueryOver.Of<Contact>()
.Select(x => x.ID)
.Where(x => x.Related.ID == emplyoee.ID); // use alias
// III. declare the outer query and use the above alias
var query = session.QueryOver<Employee>(() => emplyoee) // declare alias
.WithSubquery
.WhereExists(subQuery); // put both together
Also check this for more ideas
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;
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)