I have the LINQ to SQL as below, which works fine.
var qry = (from h in dc.Timesheets
join s in dc.Users on h.UserID equals s.UserID
join ug in dc.UserGroups on s.UserGroupID equals ug.UserGroupID
where h.BookedOn >= _dateFrom.Value && h.BookedOn <= _dateTo.Value
group h by ug.UserGroupID into g
orderby g.Count() descending
select new
{
UserGroupingID = g.Key,
BookingsPerDay = g.Count() / days
}).ToList();
Now I want to add the name of the User Group, but somehow I struggle to get the LINQ right.
My limited knowledge tells me I should add the Description to the Group clause as follow, but it's a no-go:
I try:
var qry = (from h in dc.Timesheets
join s in dc.Users on h.UserID equals s.UserID
join ug in dc.UserGroups on s.UserGroupID equals ug.UserGroupID
where h.BookedOn >= _dateFrom.Value && h.BookedOn <= _dateTo.Value
group h, GroupDescription = ug.Description by ug.UserGroupID into g
orderby g.Count() descending
select new
{
UserGroupingID = g.Key,
Description = g.Key.GroupDescription
BookingsPerDay = g.Count() / days
}).ToList();
Error: Cannot convert lambda expression to type
'System.Collections.Generic.IEqualityComparer' because it is not
a delegate type
I think you might missing the new keyword here!!
var qry = (from h in dc.Timesheets
join s in dc.Users on h.UserID equals s.UserID
join ug in dc.UserGroups on s.UserGroupID equals ug.UserGroupID
where h.BookedOn >= _dateFrom.Value && h.BookedOn <= _dateTo.Value
group new {h, GroupDescription = ug.Description} by new {GroupDescription} into g
orderby g.Count() descending
select new
{
UserGroupingID = g.Key,
Description = g.Key.GroupDescription
BookingsPerDay = g.Count() / days
}).ToList();
Shouldn't it be an == for the GroupDescription? And aren't you missing a comma in the select new statement?
Related
I have a simple linq query:
var ordersList = (from salesOrder in context.SalesOrders
where salesOrder.date >= fromDate && salesOrder.date <= thruDate
join orderJoin in context.Orders on salesOrder.id equals orderJoin.id into orderGroup
select new
{
OrderGroup = orderGroup
}).ToList();
but I'm getting could not be translated error. How can I join into a group and select that group?
I am searching for equivalent to this query:
SELECT Prd.barcode , SumQUA = SUM(AType.unit*MIA.quantity)
FROM [dbo].[MerchantInventoryActivity] AS MIA
JOIN [dbo].[MerchantInventoryActivityType] AS AType ON (MIA.typeId = AType.id)
JOIN [dbo].[Product] AS Prd ON (MIA.productId = Prd.id)
WHERE MIA.invoiceId = '123'
GROUP BY Prd.barcode
i got stuck at the SumQUA = SUM(AType.unit*MIA.quantity) . Here's what I got so far:
(from mia in Entities.MerchantInventoryActivity
join Prd in Entities.Product on mia.productId equals Prd.id
join AType in Entities.MerchantInventoryActivityType on mia.typeId equals AType.id
where mia.invoiceId == invoiceId
group Prd by Prd.barcode into g
select new
{
Barcode = g.Key,
SumQUA = // ??
}).ToList();
Thank you!
You can project first to anonymous type and then group:
(from mia in Entities.MerchantInventoryActivity
join Prd in Entities.Product on mia.productId equals Prd.id
join AType in Entities.MerchantInventoryActivityType on mia.typeId equals AType.id
where mia.invoiceId == invoiceId
select new { Prd.barcode, AType.unit, MIA.quantity } into temp
group temp by temp.barcode into g
select new
{
Barcode = g.Key,
SumQUA = g.Sum(c => c.unit * c.quantity)
});
Add check for null when sum
(from mia in Entities.MerchantInventoryActivity
join Prd in Entities.Product on mia.productId equals Prd.id
join AType in Entities.MerchantInventoryActivityType on mia.typeId equals AType.id
where mia.invoiceId == invoiceId
select new { Prd.barcode, AType.unit, MIA.quantity } into temp
group temp by temp.barcode into g
select new
{
Barcode = g.Key,
SumQUA = g.Sum(c => c.unit??0 * c.quantity??0)
});
var query = from c in context.Auto
join r in context.Label on c.idAutoChar equals r.idAutoChar
join g in context.Rent on c.idAuto equals g.idAuto
where c.idAutoChar == r.idAutoChar && c.idAuto == g.idAuto
group new {c, r, g} by new {c.idAuto, r.Name, c.RegNumber, g.Summ} into v
select new
{
v.Key.idAuto,
v.Key.Name,
v.Key.RegNumber,
Sum = (from b in v select v.Key.Summ).Sum()
};
I need to group Cars by idAuto with a column of SUM(Summ) but it didn't work correctly. Could you please help.
var query = from c in context.Auto
join r in context.Label on c.idAutoChar equals r.idAutoChar
join g in context.Rent on c.idAuto equals g.idAuto
//where c.idAutoChar == r.idAutoChar && c.idAuto == g.idAuto //???
group new {c, r, g} by new {c.idAuto, r.Name, c.RegNumber} into v
select new
{
v.Key.idAuto,
v.Key.Name,
v.Key.RegNumber,
Sum = v.Sum(item => item.Key.Summ)
};
use Sum = v.Sum(item => item.Key.Summ) and don't group by Summ, otherwise it will produce a group item for each different Summ value.
BTW, why do you need your where condition - it replicates on conditions of your joins
Assuming that g.Summ is a numeric value you want to sum on, and not something you actually want to group on then it shouldn't be part of the key.
I've no way of testing this, but something like this might be the answer:
c in context.Auto
join r in context.Label on c.idAutoChar equals r.idAutoChar
join g in context.Rent on c.idAuto equals g.idAuto
where c.idAutoChar == r.idAutoChar && c.idAuto == g.idAuto
group new {c, r, g} by new {c.idAuto, r.Name, c.RegNumber} into v
select new
{
v.Key.idAuto,
v.Key.Name,
v.Key.RegNumber,
Sum = (from b in v
select b.Summ).Sum()
};
Note we don't include c.Summ in the group by, and the Sum is then over b.Summ. I also think that g (Rent?) isn't actually required in this linq statement.
I have this Linq query which works fine
var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by c.Date.Day into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()
}).ToList();
But I want to group these results by the DayOfWeek Property, and it seems like Linq doesn't allow it since I get this error when I replace group c by c.Date.Day by group c by c.Date.DayOfWeek :
The specified type member 'DayOfWeek' is not supported in LINQ to Entities
Is there any workaround to this problem ?
Use SqlFunctions.DatePart:
var test = (from c in context.ConsumptionSet
join pi in context.PropertiesInstanceSet on c.PropertiesInstanceID equals pi.PropertiesInstanceID
join ep in context.EquipmentPropertiesSet on pi.EquipmentPropertiesID equals ep.EquipmentPropertiesID
where (ep.EquipmentID == equipmentId && pi.ProprietesName == ProprietesName.Energy && c.Date <= DateTime.Today && c.Date >= EntityFunctions.AddDays(DateTime.Today, -7))
group c by SqlFunctions.DatePart("weekday", c.Date) into grp
select new
{
test = grp.Key,
value = (from c2 in grp select c2.Value).Max()
}).ToList();
Given the following:
DP_DatabaseTableAdapters.EmployeeTableAdapter employeetableAdapter = new DP_DatabaseTableAdapters.EmployeeTableAdapter();
DP_Database.EmployeeDataTable employeeTable = employeetableAdapter.GetData();
var leadEmployees = from e in employeeTable
where e.IsLead == true
select e;
DP_DatabaseTableAdapters.LaborTicketTableAdapter tableAdapter = new DP_DatabaseTableAdapters.LaborTicketTableAdapter();
DP_Database.LaborTicketDataTable table = tableAdapter.GetDataByDate(date.ToString("MM/dd/yyyy"));
var totHours = from l in table
join e in leadEmployees on l.EmployeeID equals e.EmployeeID
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};
Total hours correctly filters the results based on the leadEmployee's list of people who have the IsLead bit set to true.
I would like to know how to do this with a where clause, I have attempd to use leadEmployees.Contanis but it wants a whole EmployeeRow...
How can I add what looks to be part of an IN clause to a where filter to replace the join?
var totHours = from l in table
where ??????
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};
The contains will only want a whole EmployeeRow if you are selecting whole employee roles. You can either:
leadEmployees.Select(e => e.id).contains
OR
leadEmployees.Count(e => e.id == l.id) > 0
Both will work. (Excuse slightly rushed lack of consideration for syntax accuracies).
This should work:
var leadEmployees = from e in employeeTable
where e.IsLead == true
select e.EmployeeID;
var totHours = from l in table
where leadEmployees.Contains(l.EmployeeID)
group l by l.EmployeeID into g
orderby g.Key
select new
{
EmployeeID = g.Key,
HoursWorked = g.Sum(s => s.HoursWorked)
};