I have the following Linq expression:
var employeeTypes = from t in DbContext.Set<SetupEmployeeType>().AsNoTracking()
join emp in DbContext.Set<Employee>().AsNoTracking() on t.EmployeeTypeId equals emp.EmployeeTypeId into employee
from subemp in employee.DefaultIfEmpty()
where t.MasterEntity == masterEntity
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = t.EmployeeTypeId,
Description = t.Description,
AllowProbation = t.AllowProbation,
IsActive = t.IsActive,
TotalEmployee = (subemp == null ? 0 : subemp.Count)
};
I need to set TotalEmployee property of my custom model.
So if there is no EmployeeTypeId associated to any Employee then TotalEmployee should be 0, else should be the Count of Employees.
Any clue how to do this?
If i may not wrong then consider using a subquery, like so -
var q = from empType in DbContext.Set<SetupEmployeeType>().AsNoTracking()
let empCount =
(
from emp in DbContext.Set<Employee>().AsNoTracking()
where empType.EmployeeTypeId == emp.EmployeeTypeId
select emp
).Count()
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = empType.EmployeeTypeId,
Description = empType.Description,
AllowProbation = empType.AllowProbation,
IsActive = empType.IsActive,
TotalEmployee = empCount
};
Using Group by.
var q = from empType in DbContext.Set<SetupEmployeeType>().AsNoTracking()
join empCnt in
(
from emp in DbContext.Set<Employee>().AsNoTracking()
group emp by emp.EmployeeTypeId into grp
select new { EmployeeTypeId = grp.Key, TotalEmp = grp.Count()}
) on empType.EmployeeTypeId equals empCnt.EmployeeTypeId into employees
from subemp in employees.DefaultIfEmpty()
where t.MasterEntity == masterEntity
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = empType.EmployeeTypeId,
Description = empType.Description,
AllowProbation = empType.AllowProbation,
IsActive = empType.IsActive,
TotalEmployee = subemp.TotalEmp
};
I came up with the following solution:
var employeeTypes = from t in DbContext.Set<SetupEmployeeType>().AsNoTracking()
join empg in
(
from emp in DbContext.Set<Employee>().AsNoTracking()
group emp by emp.EmployeeTypeId into g
select new { EmployeeTypeId = g.Key, Total = g.Count() }
) on t.EmployeeTypeId equals empg.EmployeeTypeId into employee
from subemp in employee.DefaultIfEmpty()
where t.MasterEntity == masterEntity
select new Model.SetupEmployeeTypeModel()
{
EmployeeTypeId = t.EmployeeTypeId,
Description = t.Description,
AllowProbation = t.AllowProbation,
IsActive = t.IsActive,
TotalEmployee = subemp.Total
};
Related
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
};
I use fluent API and Ef Core.
This is my linq:
var data = (from candidateHP in _cempContexto.CandidateHiringProcessSummary
join jobsHP in _cempContexto.JobHiringProcessSummary on candidateHP.JobCode equals jobsHP.JobCode
join job in _cempContexto.Vaga on jobsHP.JobCode equals job.Codigo
join candidateJob in _cempContexto.TrCandidatoVaga on new { X = job.Codigo, Y = candidateCode } equals new { X = candidateJob.VagaCodigo, Y = candidateJob.CandidatoCodigo }
where
candidateHP.CandidateCode == candidateCode
select
new
{
JobCode = jobsHP.JobCode,
CompanyCode = job.EmpresaCodigo,
Title = job.Titulo,
HasImage = true,
CompanyName = job.NomeEmpresa,
QuantityJobs = job.QuantidadeVaga,
Location = job.CidadeCodigo,
QuantityResumeSent = jobsHP.QuantityResumeSent,
JobStatusCode = jobsHP.StatusCode,
CandidateStatusCode = candidateHP.StatusCode,
StatusCode = job.StatusCodigo,
ResumeSentDate = candidateJob.Data,
InsertDate = job.DataDeCadastro,
EndDate = job.DataDeSaida,
city= _cempContexto.TrVagaCidade.SelectMany(p => _cempContexto.TrVagaCidade.Where(q => q.VagaCodigo == candidateHP.JobCode).Select(q => new { q })).ToList()
})
.ToList();
I need to Fill city with that subquery. I need a better way to do that, How could I proceed?
Split code into two queries :
var query = (from candidateHP in _cempContexto.CandidateHiringProcessSummary
join jobsHP in _cempContexto.JobHiringProcessSummary on candidateHP.JobCode equals jobsHP.JobCode
join job in _cempContexto.Vaga on jobsHP.JobCode equals job.Codigo
join candidateJob in _cempContexto.TrCandidatoVaga on new { X = job.Codigo, Y = candidateCode } equals new { X = candidateJob.VagaCodigo, Y = candidateJob.CandidatoCodigo }
where
candidateHP.CandidateCode == candidateCode
select
new
{ jobsHP = jobsHP, job = job, candidateJob = candidateJob}).ToList();
var data = query.Select(x => new {
JobCode = x.jobsHP.JobCode,
CompanyCode = x.job.EmpresaCodigo,
Title = x.job.Titulo,
HasImage = true,
CompanyName = x.job.NomeEmpresa,
QuantityJobs = x.job.QuantidadeVaga,
Location = x.job.CidadeCodigo,
QuantityResumeSent = x.jobsHP.QuantityResumeSent,
JobStatusCode = x.jobsHP.StatusCode,
CandidateStatusCode = x.candidateHP.StatusCode,
StatusCode = x.job.StatusCodigo,
ResumeSentDate = x.candidateJob.Data,
InsertDate = x.job.DataDeCadastro,
EndDate = x.job.DataDeSaida,
city= query.Select(y => y.candidateHP.JobCode).Select(q => new { q })).ToList()
})
.ToList();
I have tow tables, tblItem and tblInsertLines, in tblInsertLines I have the same ItemId but with differnt ProdDate and ExpireDate, I want to get a distinct list of all items but select the first row from tblInsertLines as the first row contains the oldest ProdDate.
Any help will be appreciated. I use this code.
public static List<Item> getItemList()
{
using (var db = new AWarehouseDataClassesDataContext())
{
var list = (from i in db.tblItems
join e in db.tblInsertLines on i.ItemId equals e.ItemId
orderby i.NameE
select new Item
{
code = i.Code,
itemId = i.ItemId,
lastUpdate = i.LastUpdate,
nameA = i.NameA,
nameE = i.NameE,
qty = i.Qty,
prodDate = e.ProdDate,
expireDate = e.ExpireDate,
updatedBy = i.UpdatedBy
}).Distinct();
return list.ToList();
}
}
You can try
var list= (from i in db.tblItems
join e in db.tblInsertLines on i.ItemId equals e.ItemId
where e.counter > 0
orderby i.NameE
group new { i, e } by e.ItemId into g
select new Item
{
code = g.First().i.Code,
itemId = g.Key,
lastUpdate = g.First().i.LastUpdate,
nameA = g.First().i.NameA,
nameE = g.First().i.NameE,
qty = g.First().i.Qty,
prodDate = g.Min(x=>x.e.ProdDate),
expireDate = g.First().e.ExpireDate,
updatedBy = g.First().i.UpdatedBy
}).ToList();
I have the following query in controller and I want to store a column value in a variable but I am not being able to iterate it. Here is my code:
var srmas = (
from SRMAs in db.SRMAs
join SRMAStatus in db.SRMAStatus on SRMAs.Status equals SRMAStatus.Id
join PurchaseOrders in db.PurchaseOrders on SRMAs.PONumber equals PurchaseOrders.PONumber
join Suppliers in db.Suppliers on PurchaseOrders.SupplierID equals Suppliers.SupplierID
join SRMADetails in db.SRMADetails on SRMAs.Id equals SRMADetails.SRMAId
where(SRMAs.Id == srmaid)
group SRMADetails by new
{
SRMADetails.Id,
SRMADetails.SRMAId,
SRMADetails.SupplierPartNum,
SRMAs.PONumber,
SRMAs.ActualAmount,
SRMAs.ApprovedOn,
SRMAs.Status,
SRMAs.TrackingNumber,
SRMAs.SupplierRMANumber,
SRMAs.RequestedFromSupp,
SRMAs.CreatedOn,
Suppliers.SupplierName,
SRMAStatus.StatusName,
PurchaseOrders.PODate,
PurchaseOrders.suppliersOrderNumber
} into grp
select new
{
grp.Key.Status,
grp.Key.SRMAId,
grp.Key.Id,
grp.Key.PONumber,
grp.Key.SupplierRMANumber,
grp.Key.ActualAmount,
grp.Key.SupplierPartNum,
grp.Key.RequestedFromSupp,
grp.Key.TrackingNumber,
grp.Key.ApprovedOn,
grp.Key.SupplierName,
grp.Key.StatusName,
grp.Key.PODate,
grp.Key.suppliersOrderNumber,
grp.Key.CreatedOn,
Sum = grp.Sum(SRMADetails => SRMADetails.Cost * SRMADetails.QtyReturned)
}
).ToList();
System.Collections.IEnumerable et = (System.Collections.IEnumerable)srmas;
IEnumerator it = et.GetEnumerator();
while (it.MoveNext())
{
SRMA current = (SRMA)it.Current;
Response.Write(current.Status);
}
ViewBag.SRMAs = srmas.Select(srma => new IndexViewModel
{
Id = srma.SRMAId,
SupplierRMANum = srma.SupplierRMANumber,
SRMADetailsID = srma.Id,
PONumber = srma.PONumber,
CreatedOn = srma.CreatedOn,
SupplierName = srma.SupplierName,
SRMAStatus = srma.StatusName,
Status = srma.Status,
suppliersOrderNumber = srma.suppliersOrderNumber,
PODate = srma.PODate,
Sum = srma.Sum,
TrackingNumber = srma.TrackingNumber,
ActualAmount = srma.ActualAmount
}).ToList();
I just want to get Status value of first record. How do I do it?
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
});