How to write IQueryable join in Entity Framework 6 - c#

I do have this code but I want to do it in IQueryable:
var select = (from g in this.context.Grades
join sm in this.context.SubjectMaintenance on g.SubjectCode equals sm.SubjectCode
join st in this.context.Students on g.StudentId equals st.StudentId
where sm.TeacherId == sTeacherId
select new
{
SubjectCode = g.SubjectCode,
SubjectName = g.SubjectName,
StudentName = st.LastName + ", " + st.Firstname,
PrelimGrade = g.PrelimGrade,
MidtermGrade = g.PrelimGrade,
FinalGrade = g.PrelimGrade,
}).ToList();
How do I write it using IQueryable? My start code for IQueryable is this:
IQueryable<Grades> query = this.context.Grades;
IQueryable<SubjectMaintenance> query1 = this.context.SubjectMaintenance;
IQueryable<Students> query2 = this.context.Students;
Please do help as I need it in my project. Thank you in advance for your help.

You can follow the below logic to write your query.
var query1 =
var query2 =
var join = query1.Join(query2, x => x.ParentId, y => y.ParentId, (query1, query2) => new { query1, query2 })
Please follow this document also.

Related

Is there a way to write this LINQ query using lambda expressions?

I have a linq query that I use to get data from 3 tables using 2 joins. I would prefer to write this as a lambda expression so my team finds it more readable. Is there a way to do this though? I can't find any decent examples.
var q = (
from sus in susManager.Get()
join su in suManager.Get() on sus.SUId equals su.Id
join p in pManager.Get() on su.PId equals p.Id
where sus.EndTimeStamp >= oneDayAgo
select new
{
Name = p.FirstName + " " + p.LastName,
Email = su.Email,
LastLogIn = sus.StartTimeStamp,
LastSessionDurationInMinutes =
DbFunctions.DiffMinutes(sus.StartTimeStamp, sus.EndTimeStamp),
LastActive = sus.EndTimeStamp
}).ToList();
The lambda equivalent would look something like this, assuming that the Get() functions return Lists:
var q = systemUserSessionManager.Get()
.Where(sus => sus.EndTimeStamp >= oneDayAgo)
.Join(systemUserManager.Get(),
sus => sus.SystemUserId,
su => su.Id,
(sus, su) => new { sus, su })
.Join(personManager.Get(),
j => j.su.PersonId,
p => p.Id,
(j, p) => new { sus = j.sus, su = j.su, p })
.Select(x => new
{
Name = p.FirstName + " " + p.LastName,
Email = su.Email,
LastLogIn = sus.StartTimeStamp,
LastSessionDurationInMinutes =
DbFunctions.DiffMinutes(sus.StartTimeStamp, sus.EndTimeStamp),
LastActive = sus.EndTimeStamp
})
.ToList();
Try something like this
var q = (
from sus in systemUserSessionManager.Get()
join su in systemUserManager.Get() on sus.SystemUserId equals su.Id
join p in personManager.Get() on su.PersonId equals p.Id
select new { sus = sus, su = su, p = p})
.Where(x => x.sus.EndTimeStamp >= oneDayAgo)
.Select(x => new {
Name = x.p.FirstName + " " + x.p.LastName,
Email = x.su.Email,
LastLogIn = x.sus.StartTimeStamp,
LastSessionDurationInMinutes =
DbFunctions.DiffMinutes(x.sus.StartTimeStamp, x.sus.EndTimeStamp),
LastActive = x.sus.EndTimeStamp
}).ToList();

How to convert to Linq

How to convert the following query into linq
SELECT
a.ProductId,
a.Name,
a.Description,
b.Quoteid,
b.Productid,
b.Quantity,
b.OriginalPrice
FROM
Products AS a
LEFT JOIN
QuoteDtails AS b
ON a.ProductId = b.ProductId
AND b.QuoteId = 200;
Don't know where to add the AND condition.
Thanks and regards
You can try this linq if you want to write LEFT JOIN of linq, you need to add
into [temp collection] from [Left join talbe collection] in [temp collection].DefaultIfEmpty()
after Linq join
look like this.
from ss in Products
join aa in QuoteDtails
on ss.ProductId equals aa.ProductId into temp
from ds in temp.DefaultIfEmpty()
where ds.QuoteId = 200
select new
{
ProductId_P = ss.ProductId,
Name = ss.Name,
Description = ss.Description,
Quoteid = ds.Quoteid,
Productid_Q = ds.Productid,
Quantity = ds.Quantity,
OriginalPrice = ds.OriginalPrice
}
You can add AND condition in your LINQ query like this :
var res = from p in products
join q in quoteDtails on new { Criteria1 = p.ProductID, Criteria2 = 200 } equals new { Criteria1 = q.Productid, Criteria2 = q.Quoteid }
select new
{
ProductId_P = p.ProductID,
Name = p.Name,
Description = p.Description,
Quoteid = q.Quoteid,
Productid_Q = q.Productid,
Quantity = q.Quantity,
OriginalPrice = q.OriginalPrice
};

C# LINQ: Get count for each row

Here the query for retrieving all the comptes in my base.
I would like to add a number within the object GridCompte in order to count the element inside the list (1,2,3...):
var comptes = (from c in Comptes
join s in Societies on c.IdSoc equals s.IdSoc
select new GridCompte
{
SocCompteId = c.IdCompte,
Name = c.Name,
Nb = ??? COUNT ???,
.....
SocName = s.Name
}).ToList();
I tried using the group statement, but i didn't manage to achieve my goal.
Any suggestions?
First prepare your linq only for the fields you want to get
var comptes = from c in Comptes
join s in Societies on c.IdSoc equals s.IdSoc
select new
{
SocCompteId = c.IdCompte,
Name = c.Name,
.....
SocName = s.Name
};
Now use index option that is available in Select
var finalComptes = (comptes.AsEnumerable()
.Select((comptes, index) => new GridCompte()
{
SocCompteId = c.IdCompte,
Name = c.Name,
Nb = index + 1,
.....
SocName = s.Name
}).ToList();

LINQ to Dataset SELECT NOT IN to a table?

Here is my working LINQ to dataset query. I have added a fourth table and I want to add a NOT IN type filter to the query where query1.dedpk NOT IN deduct.dedpk. Not sure how to do it.
var main = dsTemp.Tables["Maindata"].AsEnumerable();
var pg = dsTemp.Tables["pgto"].AsEnumerable();
var py = dsTemp.Tables["pyto"].AsEnumerable();
var deduct = dsTemp.Tables["Deduct"].AsEnumerable();
var query1 = from mainrow in main
join pgrow in pg on mainrow.Field<string>("pgpk") equals pgrow.Field<string>("pgpk")
join pyrow in py on mainrow.Field<string>("pypk") equals pyrow.Field<string>("pypk")
into griddata
select new
{
lastname = mainrow.Field<string>("lastname"),
firstname = mainrow.Field<string>("firstname"),
dedpk = mainrow.Field<string>("dedpk"),
};
Thanks
var query1 = from mainrow in main
join pgrow in pg on mainrow.Field<string>("pgpk") equals pgrow.Field<string>("pgpk")
join pyrow in py on mainrow.Field<string>("pypk") equals pyrow.Field<string>("pypk")
into griddata
where
!deduct.Any(x => x.Field<string>("dedpk") == mainrow.Field<string>("dedpk"))
select new
{
lastname = mainrow.Field<string>("lastname"),
firstname = mainrow.Field<string>("firstname"),
dedpk = mainrow.Field<string>("dedpk"),
};

How to generate SQL COUNT(*) OVER (PARTITION BY {ColumnName}) in LINQ-to-SQL?

Is it possible to generate the following SQL query by using LINQ-to-SQL query expression or method chains which is defer-executable?
Data Structure
alt text http://www.freeimagehosting.net/uploads/e062a48837.jpg
Select Distinct ClassRoomTitle,
Count(*) Over(Partition By ClassRoomNo) As [No Sessions Per Room],
TeacherName,
Count(*) Over(Partition By ClassRoomNo, TeacherName) As [No Sessions Per Teacher] From ClassRoom
Expected Result
alt text http://www.freeimagehosting.net/uploads/47a79fea8b.jpg
Try this:
var vGroup = from p in ClassRoom
group p by new { p.ClassRoomNo, p.TeacherName }
into g
from i in g
select new
{
i.ClassRoomNo,
i.TeacherName,
i.ClassRoomTitle,
NoSessionsPerTeacher = g.Count()
};
var pGroup = from p in vGroup
group p by new { p.ClassRoomNo }
into g
from i in g
select new
{
i.ClassRoomTitle,
NoSessionsPerRoom = g.Count(),
i.TeacherName,
i.NoSessionsPerTeacher
};
var result = pGroup.OrderBy(p => p.ClassRoomNo).ThenBy(p => p.TeacherName);
I didn't test the above but you can check my original code in case I got something wrong in the rewrite:
var vGroup = from p in Products
group p by new { p.ProductId, p.VariantId }
into g
from i in g
select new
{
i.ProductId,
i.VariantId,
VariantCount = g.Count()
};
var pGroup = from p in vGroup
group p by new { p.ProductId }
into g
from i in g
select new
{
i.ProductId,
ProductCount = g.Count(),
i.VariantId,
i.VariantCount
};
var result = pGroup.OrderBy(p => p.ProductId).ThenBy(p => p.VariantId);
var classRooms = from c in context.ClassRooms
group c by new {c.ClassRoomNo} into room
select new {
Title = room.First().ClassRoomTitle,
NoSessions = room.Count(),
Teachers = from cr in room
group cr by new {cr.TeacherName} into t
select new {
Teacher = t.Key,
NoSessions = t.Count()
}
};
A bit more structured than the posted expected result, but I find that to be better.
You can always use SelectMany if you want to go back to unstructured:
var unstructured = classRooms
.SelectMany(c=> c.Teachers.Select( t=> new {
Title = c.Title,
SessionsPerRoom = c.NoSessions,
Teacher = t.Teacher,
SessionsPerTeacher = t.NoSessions
});

Categories

Resources