transform sql to linq with join, group-by and where - c#

I have the following sql, which I want to convert to linq
SELECT Contrato.finca, SUM(Pago.Importe_subtotal)
FROM Pago, Contrato
WHERE Pago.Contrato = Contrato.ID AND Pago.pagado = 1
GROUP BY Contrato.finca
ORDER BY 2 DESC
GO
What I have now in linq is the following, but the group by doesn't work.
var x = from contrato in ctx.Contratos
join pago in ctx.Pagos
on contrato.ID equals pago.Contrato
where pago.pagado == true
group contrato by contrato.finca
select contrato.Finca1;

Think this should work:
ctx.Pagos.Where(m=>m.pagado==true).GroupBy(m=>m.Contrato.finca)
.Select(m=> new { Id= m.Key, Val= m.Sum(n => n.Importe_subtotal)})

Try
var x = from contrato in ctx.Contratos
join pago in ctx.Pagos
on contrato.ID equals pago.Contrato
where pago.pagado == true
group new {contrato, pago} by contrato.finca into g
select new {
key = g.Key,
sum = g.Sum(p => p.pago.Importe_subtotal)
};

Related

Add Max and Average in linq query

I've got an sql query below:
SELECT MAX(r.Suburb), AVG(s.Longitude) AS Longitude, AVG(s.Latitude) AS Latitude
From CafeAddress r inner join Cafe s on s.CafeId = r.CafeId
WHERE r.Region = 'Sydney'
which outputs:
Bankstown 174.759541622222 -36.8552809611111
The query should be getting all suburbs in the region e.g. Sydney is the region and suburbs would include Bankstown etc..
How would I add max and average in my linq query below? (region id is a user input (string))
var region = from cafeAddress in _context.CafeAddress
join cafe in _context.Cafe on cafeAddress.CafeId equals cafe.CafeId
where cafeAddress.StateProvinceRegion == regionId
select new { StateProvinceRegion = cafeAddress.Suburb.Max(), cafe.Latitude, cafe.Longitude };
I'm only able to do it through using a group by but I'm trying to avoid that.
I think this would be getting fairly close:
var query =
from cafeAddress in _context.CafeAddress
join cafe in _context.Cafe on cafeAddress.CafeId equals cafe.CafeId
where cafeAddress.StateProvinceRegion == regionId
orderby cafeAddress.Suburb descending
group new
{
cafeAddress.Suburb,
cafe.Latitude,
cafe.Longitude
} by cafeAddress.StateProvinceRegion into g
select new
{
StateProvinceRegion = g.First().Suburb,
Latitude = g.Average(x => x.Latitude),
Longitude = g.Average(x => x.Longitude),
};
Or this slight variant:
var query =
from cafeAddress in _context.CafeAddress
join cafe in _context.Cafe on cafeAddress.CafeId equals cafe.CafeId
where cafeAddress.StateProvinceRegion == regionId
orderby cafeAddress.Suburb descending
group new
{
cafeAddress.Suburb,
cafe.Latitude,
cafe.Longitude
} by cafeAddress.StateProvinceRegion into gs
from g in gs.Take(1)
select new
{
StateProvinceRegion = g.Suburb,
Latitude = gs.Average(x => x.Latitude),
Longitude = gs.Average(x => x.Longitude),
};

How to rewrite sql query to linq

I'm trying to rewrite sql query to linq but can't do it myself.
The most problem for me is to get I,II and III aggregated values.
Sql query:
select o.Name,t.TypeID, SUM(e.I),SUM(e.II),SUM(e.III) from Expenditure e
join Finance f on f.FinanceId = e.FinanceId
join FinanceYear fy on fy.FinanceYearId = f.FinanceYearId and fy.StatusId = 1
join Project p on p.ProjectId = fy.ProjectId
join Organization o on o.OrganizationId = p.OrganizationId
join Type t on t.TypeID = p.TypeID
where fy.Year = 2018
group by o.Name,s.TypeID
and what I have done so far is:
var x = (from e in _db.Expenditures
join f in _db.Finances on e.FinanceId equals f.FinanceId
join fy in _db.FinanceYears on f.FinanceYearId equals fy.FinanceYearId and fy.StatusId = 1 // this does not work, cant join on multiple conditions?
join p in _db.Projects on fy.ProjectId equals p.ProjectId
join o in _db.Organizations on p.OrganizationId equals o.OrganizationId
join s in _db.Types on p.TypeId equals s.TypeId
group new { o, s } by new { o.OrganizationId, s.TypeId }
into grp
select new AggModel
{
OrganizationId = grp.Key.OrganizationId,
TypeId = grp.Key.TypeId,
I = ?,
II = ?,
III = ?,
}
);
Try something like this:
group new { e, o, s } by new { o.OrganizationId, s.TypeId }
into grp
select new AggModel
{
OrganizationId = grp.Key.OrganizationId,
TypeId = grp.Key.TypeId,
I = grp.Sum(a => a.e.I),
II = grp.Sum(a => a.e.II),
III = grp.Sum(a => a.e.III),
}
You'll need to adjust the right side of the lambda to navigate to the correct property.
You Need to use the Group by for aggregation methods.
Check the below link for more Knowledge.
How to use aggregate functions in linq with joins?

Linq Dynamic Query With Group By

I need extra where clause for my Linq query. For example if customer choose a date filter so i need to date filter to my query etc... When i try to myQuery.Where predicate there is visible just group by's field.
How can i append new where condition to my query.
//for example i need dynamically append o.OrderDate==Datetime.Now or another where clause
var myQuery =(from o in _db.Orders
join l in _db.OrderLines.Where(x => x.ParaBirimi == model.ParaBirimi) on o.orderId equals
l.OrderId
where o.OrderDate.Value.Year == year1
group o by new {o.OrderDate.Value.Month}
into g
select
new
{
Month = g.Key.Month,
Total = g.Select(t => t.OrderLines.Sum(s => s.OrderTotal)).FirstOrDefault()
});
You are too late at the end of the query to add new Where. You have already grouped the data, and projected it, removing nearly all the fields.
Try:
var baseQuery = from o in _db.Orders
join l in _db.OrderLines.Where(x => x.ParaBirimi == model.ParaBirimi) on o.orderId equals l.OrderId
where o.OrderDate.Value.Year == year1
select new { Order = o, OrderLine = l };
if (something)
{
baseQuery = baseQuery.Where(x => x.Order.Foo == "Bar");
}
var myQuery = (from o in baseQuery
group o by new { o.Order.OrderDate.Value.Month }
into g
select
new
{
Month = g.Key.Month,
Total = g.Sum(t => t.OrderLine.OrderTotal)
});
Clearly you can have multiple if. Each .Where() is in && (AND) with the other conditions.
Note how the result of the join is projected in an anonymous class that has two properties: Order and OrderLine

LINQ - multi join with Group by and get average

I'm trying to write this select in LINQ but Im not successful to fix it for long time. I also tried LINQ - join with Group By and get average but it doesn't work in my code. It is obviously that I'm wrong.
SQL:
SELECT name_type, AVG(t.price) as avgPrice FROM type tp
JOIN location l ON l.ID_type = tp.ID
JOIN event e ON e.ID_location = l.ID
JOIN ticket t ON t.ID_event = e.ID
GROUP BY tp.name_type
LINQ:
var q3 = from l in db.location
join tp in db.type on l.ID_type equals tp.ID
join e in db.event on l.ID equals u.ID_location
join t in db.ticket on e.ID equals t.ID_event
group tp by new {Type_name = tp.type_name} into grp
select new
{
Type_name = grp.Key.type_name,
avgPrice = grp.Average( x => x.ticket.price)
};
There are a few problems:
There is an error in the second join—I believe u.ID_location needs to be e.ID_location.
I think you are grouping on the wrong entity, try grouping by t instead of tp.
You don't need the anonymous type in the group by.
Try this:
var results =
from l in db.location
join tp in db.type on l.ID_type equals tp.ID
join e in db.event on l.ID equals e.ID_location
join t in db.ticket on e.ID equals t.ID_event
group t by new tp.type_name into grp
select new
{
Type_name = grp.Key,
avgPrice = grp.Average(x => x.price)
};
If you happen to have navigation properties set up between your entities, this would be a lot easier. It's pretty hard to tell how the entities are supposed to be related, but I'm thinking something like this would work:
// average ticket price per location type
var results =
from t in db.ticket
group t by t.event.location.type.type_name into g
select new
{
Type_name = g.Key,
avgPrice = g.Average(x => x.price)
};
Or in fluent syntax:
var results = db.ticket.GroupBy(t => t.event.location.type.type_name)
.Select(g => new
{
Type_name = g.Key,
avgPrice = g.Average(x => x.price)
});

Sub-Query in linq

What is the best way to write this query in linq?
SELECT *
FROM product_master
where product_id in (select product_id from product where buyer_user_id=12)
var _result = from a in product_master
where (product.Where(s => s.buyer_user_id == 12))
.Contains(a.product_ID)
select a;
or
var _result = (from a in product_master
join b in product
on a.product_id equals b.product_id
where b.buyer_user_id == 12
select a).Distinct();
JW's answer is correct. Alternatively, given that the subquery is not correlated, you could express it separately:
var pids = product.Where(p => p.buyer_user_id == 12);
var r = product_master.Where(pm => pids.Contains(pm.product_id));

Categories