I lost a day to try translate a sql query to LINQ lambda expression but not success.
My sql query:
SELECT a.ID,
Sum(b.[Value]) AS [Value],
c.ContractValue
FROM Contracts a
LEFT JOIN DepositHistories b
ON b.ContractID = a.ID
INNER JOIN LearningPackages c
ON a.LearningPackageID = c.ID
GROUP BY a.ID,
c.ContractValue
HAVING Sum(b.[Value]) < c.ContractValue
OR Sum(b.[Value]) IS NULL
OR Sum(b.[Value]) = 0
This is LINQ query:
var contracts = (
from a in db.Contracts
from b in db.LearningPackages.Where(e => e.ID == a.LearningPackageID).DefaultIfEmpty()
group a by new
{
a.ID,
b.ContractValue
} into g
from c in db.DepositHistories.Where(e => e.ContractID == g.Key.ID).DefaultIfEmpty()
where g.Sum(e => c.Value) < g.Key.ContractValue || g.Sum(e => c.Value) == null
select new
{
ID = g.Key.ID,
ContractValue = g.Key.ContractValue,
Value = g.Sum(e => c.Value != null ? c.Value : 0)
}
).ToList();
My result:
ID ContractValue Value
1 6000000 500000
1 6000000 500000
1 6000000 500000
1 6000000 500000
1 6000000 500000
3 7000000 500000
3 7000000 500000
3 7000000 500000
4 6000000 500000
5 6000000 0
6 6000000 0
It's not group and sum the values.
Please help me!
Thanks!
you can do it like this:
var result = from b in db.DepositHistories
join a in db.Contracts on b.CotractID equals a.ID
join c in db.LearningPackages on a.LearningPackageID equals c.ID
group b by new{ a.ID,c.COntractValue} into g
where g.Sum(x=>x.Value) < g.Key.COntractValue
|| g.Sum(x=>x.Value) == null
|| g.Sum(x=>x.Value) == 0
select new
{
ID = g.Key.ID,
Value = g.Sum(x=>x.Value),
ContractValue = g.Key.COntractValue
};
I made a DEMO FIDDLE to be more clear.
UPDATE:
For left outer join you have to do join your condition into somealias and them from alias in somealias.DefaultIfEmpty().
Here is the version with left outer join which gives correct results:
var result = from a in Contracts
join b in DepositHistories on a.ID equals b.CotractID into e
from f in e.DefaultIfEmpty()
join c in LearningPackages on a.LearningPackageID equals c.ID
group f by new
{
a.ID,
c.COntractValue
} into g
where g.Sum(x => x==null ? 0 : x.Value) < g.Key.COntractValue
|| g.Sum(x => x==null ? 0 : x.Value) == 0
select new
{
ID = g.Key.ID,
Value = g.Sum(x => x == null ? 0 : x.Value),
ContractValue = g.Key.COntractValue
};
UPDATED FIDDLE DEMO
You can also check this SO post about How to do left outer join in LINQ
UPDATE 2:
Using query method you have to use GroupJoin() method for left outer join.
Here is the above code with Method Query:
var Result = Contracts.GroupJoin(DepositHistories,
a => a.ID,
b => b.CotractID,
(a, b) => new { a = a, b = b })
.Join(LearningPackages,
a => a.a.LearningPackageID,
b => b.ID,
(a, b) => new { a = a, b = b })
.GroupBy(e => new
{
e.a.a.ID,
e.b.COntractValue
},
(k, g) => new
{
ID = k.ID,
ContractValue = k.COntractValue,
Value = g.Sum(x => x == null ? 0 : x.a.b.Sum(d=>d.Value))
}
).Where(x => x.Value < x.ContractValue || x.Value == 0).ToList();
UPDATED FIDDLE WITH METHOD QUERY
Related
I have this in my code:
var pRating = ctx.PrProjectRating
.Where(x => x.PrIsDeleted == false)
.Select(k => k)
.GroupBy(g =>
new {g.PrPIdG},
(key, group) => new
{
sumR = group.Sum(k => k.PrValue),
pidG = key.PrPIdG
});
var pLike = ctx.PlProjectLike
.Where(x => x.PlValue == "Like")
.Select(c => c)
.GroupBy(g =>
new {g.PlPIdG},
(key, group) => new
{
sumR = group.Count(),
pidG = key.PlPIdG
})
.OrderByDescending(g => g.sumR);
var pConnect = ctx.PcProjectConnect
.Where(x => x.PcStatus == "Connected")
.Select(c => c)
.GroupBy(g =>
new {g.PcPIdG},
(key, group) => new
{
sumR = group.Count(),
pidG = key.PcPIdG
})
.OrderByDescending(g => g.sumR);
How do i combine these collections and sum the sumR value together?
EDIT
pRating =
pidG sumR
123 11
124 7
125 5
pLike =
pidG sumR
123 3
125 2
pConnect =
pidG sumR
125 5
Result should be:
pResult =
pidG sumR
123 15
125 12
124 7
i need to group the pidG together and sum them up using sumR
I wanted to get the list of pidG values group them and find the count or sum and order them by the highest sum value and thats what you see in the collections above and in the table diagram.
Then i need to grab the sum and group the collections to find that its ordered by the highest value of sumR
EDIT
im trying to do this:
var query =
from i in ids
join ra in ratings on i equals ra.Id into rs
from ra in rs.DefaultIfEmpty()
join l in likes on i equals l.Id into ls
from l in ls.DefaultIfEmpty()
join co in connects on i equals co.Id into cs
from co in cs.DefaultIfEmpty()
select new
{
Id = i,
Total = ra?.Sum ?? 0 + l?.Count ?? 0 + co?.Count ?? 0,
Ratings = ra?.Sum ?? 0,
Likes = l?.Count ?? 0,
Connects = co?.Count ?? 0,
};
query = query.OrderByDescending(x => x.Total);
But does not sum the total which i need.
You should be able to just do all the queries separately as subqueries, then do a full join to combine the results on the client.
var ratings =
from r in ctx.PrProjectRating
where !r.PrIsDeleted
group r.PrValue by r.PrPIdG into g
select new
{
Id = g.Key,
Sum = g.Sum(),
};
var likes =
from l in ctx.PlProjectLike
where l.PlValue == "Like"
group 1 by l.PlPIdG into g
select new
{
Id = g.Key,
Count = g.Count(),
};
var connects =
from c in ctx.PcProjectConnect
where c.PcStatus == "Connected"
group 1 by c.PcPIdG into g
select new
{
Id = g.Key,
Count = g.Count(),
};
var ids = ratings.Select(r => r.Id)
.Union(likes.Select(l => l.Id))
.Union(connects.Select(c => c.Id))
.ToHashSet();
var query =
from i in ids
join r in ratings on i equals r.Id into rs
from r in rs.DefaultIfEmpty()
join l in likes on i equals l.Id into ls
from l in ls.DefaultIfEmpty()
join c in connects on i equals c.Id into cs
from c in cs.DefaultIfEmpty()
select new
{
Id = i,
Ratings = r?.Sum ?? 0,
Likes = l?.Count ?? 0,
Connects = c?.Count ?? 0,
};
This is my first post and I hope you can help me. I didn't find an answer so here I'm:
I created this query in SQL and it works.
string consultaSQL =
#"SELECT a.GastosEstudio - ISNULL(SUM(b.GastosEstudioR),0) AS restagastos, a.Articulo - ISNULL(SUM(b.ArticuloR),0) AS restaarticulo, a.Honorarios - ISNULL(SUM(b.HonorariosR),0) AS restahonorarios, a.IVAHonorarios - ISNULL(SUM(b.IVAHonorariosR),0) AS restaivahonorarios FROM deudores a LEFT JOIN recibos b ON a.DNI=b.DNI WHERE a.DNI = #DNI GROUP BY a.GastosEstudio, a.Articulo, a.Honorarios, a.IVAHonorarios";
Now I need to do the same but in LINQ. Basically: I have two tables (deudores and recibos). In deudores I have the debt with the different concepts (columns):
gastos, articulo, honorarios, ivahonorarios
In the table recibos I insert the receipts with the same columns.
The SQL query sums the receipts and subtracts the debt. The closest I get in LINQ was this:
var query = (from d in bd.deudores
join r in bd.recibos on d.DNI equals r.DNI
where d.DNI == DNI
group d by d.DNI into g
select new
{
DNI = g.Key,
articulo = g.Max(x => x.Articulo) - g.Sum(x => x.ArticuloR),
gastos = g.Max(x => x.GastosEstudio) - g.Sum(x => x.GastosEstudioR),
honorarios = g.Max(x => x.Honorarios) - g.Sum(x => x.HonorariosR),
ivah = g.Max(x => x.IVAHonorarios) - g.Sum(x => x.IVAHonorariosR),
});
The problem with this query is that if there is no receipt does not show any information (should show the initial debt)
I try with DefaultIfEmpty but didn't work:
var query = (from d in bd.deudores
join r in bd.recibos on d.DNI equals r.DNI into Pagos
from p in Pagos.DefaultIfEmpty()
where d.DNI == DNI
group d by d.DNI into g
select new
{
DNI = g.Key,
articulo = g.Max(x => x.Articulo) - g.SelectMany(x => x.recibos).Count() >= 1
? g.SelectMany(x => x.recibos).Sum(y => y.ArticuloR)
: 0,
gastos = g.Max(x => x.GastosEstudio) - g.SelectMany(x => x.recibos).Count() >= 1
? g.SelectMany(x => x.recibos).Sum(y => y.GastosEstudioR)
: 0,
honorarios = g.Max(x => x.Honorarios) - g.SelectMany(x => x.recibos).Count() >= 1
? g.SelectMany(x => x.recibos).Sum(y => y.HonorariosR)
: 0,
ivah = g.Max(x => x.IVAHonorarios) - g.SelectMany(x => x.recibos).Count() >= 1
? g.SelectMany(x => x.recibos).Sum(y => y.IVAHonorariosR)
: 0
});
The problem with this query is that it does not subtract it.
Any suggestion?
Thank you!
You want the equivalent of an outer join, so you correctly turn to a GroupJoin, or join ... into. But the query part ...
from d in bd.deudores
join r in bd.recibos on d.DNI equals r.DNI into Pagos
from p in Pagos.DefaultIfEmpty()
where d.DNI == DNI
group d by d.DNI into g
... does more than you want. In fluent LINQ syntax its structure is equivalent to
bd.deudores.GroupJoin(bd.recibos, ...)
.SelectMany(...)
.GroupBy(...)
The point is that the first GroupJoin creates a collection of deudores, each having a group of their recibos, that may be empty. Then the SelectMany flattens it into pairs of one deudores and one recibos or null. Subsequently, the GroupBy creates groups with null elements.
The first GroupJoin is all you need:
from d in bd.deudores
join r in bd.recibos on d.DNI equals r.DNI into g
select new
{
DNI = d.DNI,
articulo = d.Articulo - g.Select(x => x.ArticuloR).DefaultIfEmpty().Sum(),
gastos = d.GastosEstudio - g.Select(x => x.GastosEstudioR).DefaultIfEmpty().Sum(),
honorarios = d.Honorarios - g.Select(x => x.HonorariosR).DefaultIfEmpty().Sum(),
ivah = d.IVAHonorarios - g.Select(x => x.IVAHonorariosR).DefaultIfEmpty().Sum()
});
By adding DefaultIfEmpty() it is ensured that Sum will return 0 when there are no elements.
#Gert Arnold: The relationship between the two tables is a column name DNI. In the table deudores is PK and in the table recibos is FK. Last night i tried this code and it works:
var query = (from d in bd.deudores
join r in bd.recibos
on d.DNI equals r.DNI into g
where d.DNI == DNI
select new
{
articulo = d.Articulo - g.Sum(x => x.ArticuloR) ?? d.Articulo,
gastos = d.GastosEstudio - g.Sum(x => x.GastosEstudioR) ?? d.GastosEstudio,
honorarios = d.Honorarios - g.Sum(x => x.HonorariosR) ?? d.Honorarios,
ivah = d.IVAHonorarios - g.Sum(x => x.IVAHonorariosR) ?? d.IVAHonorarios
});
Is it the best way to do it ?. If you want to give me your opinion will be welcome.
Regards!
Trying to convert the below SQL query to LINQ, but I'm stuck at grouping by ClientCompany.
SELECT TOP 300 ClientCompany,
CASE WHEN MIN(FeatureID) = 12 THEN 1 ELSE 0 END as Sort
FROM Ad
LEFT JOIN AdFeature
ON Ad.ID = AdFeature.AdID
WHERE (AdFeature.FeatureID = 13 OR AdFeature.FeatureID = 12)
AND SiteID = 2
GROUP BY ClientCompany
ORDER BY Sort DESC
My attempt to convert this to LINQ:
(from a in Ads
join af in AdFeatures
on new {
join1 = a.ID,
join3 = 2
} equals new {
join1 = af.AdID,
join3 = af.SiteID
}
let sort = (
af.FeatureID == 12 ? 1 : 0
)
orderby sort descending
where af.FeatureID == 13 || af.FeatureID == 12
select new { a.ClientCompany, sort } ).Take(300)
How would I use MIN(FeatureID) and GROUP BY ClientCompany in LINQ, so that I only get a single row per ClientCompany back?
EDIT
This worked! Based on Daniel Hilgarth's answer. Is there anything that can go horribly wrong with this solution?
Ads.Join(AdFeatures, x => x.ID, x => x.AdID,
(a, af) => new { Ad = a, AdFeature = af })
.Where(x => x.AdFeature.FeatureID == 12 || x.AdFeature.FeatureID == 13)
.Where(x => x.AdFeature.SiteID == 2)
.GroupBy(x => x.Ad.ClientCompany)
.Select(g => new { ClientCompany = g.Key, Sort = g.Min(x => x.AdFeature.FeatureID) == 12 ? 1 : 0 })
.OrderByDescending(x => x.Sort)
.Take(300)
Try this:
Ads.Join(AdFeatures, x => x.FeatureID, x => x.FeatureID,
(a, af) => new { Ad = a, AdFeature = af })
.Where(x => x.AdFeature.FeatureID == 12 || x.AdFeature.FeatureID == 13)
.Where(x => x.AdFeature.SiteID == 2)
.GroupBy(x => x.Ad.ClientCompany)
.Select(g => new { ClientCompany = g.Key,
Sort = g.Min(x => x.AdFeature.FeatureID) == 12 ? 1 : 0 });
Please note, I changed the left outer join into an inner join, because your original query accesses AdFeature unconditionally, making it effectively an inner join .
hi I would write it like that
context.Ads.Where(ad => ad.AdFeatures.Any(feature => (feature.FeatureID == 13 || feature.FeatureID == 12) && feature.SiteID == 2))
.GroupBy(ad => ad.ClientCompany)
.Select(ads => new
{
cc = ads.Key, sort = ads.SelectMany(ad => ad.AdFeatures)
.Select(feature => feature.FeatureID)
.Min() == 12
})
.OrderBy(arg => arg.sort).Take(300);
Try this:
(from a in ads
join af in AdFeatures on a.ID equals af.AdID into g
from x in g.DefaultIfEmpty()
where x.FeatureID == 13 || x.FeatureID == 12
where x.SiteID == 2
orderby a.Sort descending
group a by a.ClientCompany into g2
from x2 in g2
let sort = g2.Select(T => T.FeatureID).Min() == 12 ? 1 : 0
select new { a.ClientCompany, Sort = sort }).Take(300);
Why do you need grouping anyway?
I have this sql query that does exactly what i want but i need it in linq. It returns a few AVC rows and counts how many PersonAVCPermission that has status 1 linked to it
SELECT a.Id, a.Name, a.Address, COUNT(p.AVCID) AS Count
FROM AVC AS a
LEFT OUTER JOIN
(
SELECT PersonAVCPermission.AVCId
FROM PersonAVCPermission
WHERE PersonAVCPermission.Status = 1
) AS p
ON a.Id = p.AVCId
GROUP BY a.Id, a.Name, a.Address
I have this query in linq and it does the same thing except when there are no PersonAVCPermission it still counts 1
var yellows = odc.PersonAVCPermissions.Where(o => o.Status == (int)AVCStatus.Yellow);
var q = from a in odc.AVCs
from p in yellows.Where(o => o.AVCId == a.Id).DefaultIfEmpty()
group a by new { a.Id, a.Name, a.Address } into agroup
select new AVCListItem
{
Id = agroup.Key.Id,
Name = agroup.Key.Name,
Address = agroup.Key.Address,
Count = agroup.Count(o => o.Id != null)
};
Im guessing that with DefaultIfEmpty() it places null rows in the list that then gets counted so i try to exclude them with (o => o.Id != null) but it still counts everything as at least one
If i dont use DefaultIfEmpty() it skips the rows with count 0 completely
How can i exclude them or am i doing it completely wrong?
How about using .Any() and a Let?
var yellows = odc.PersonAVCPermissions.Where(o => o.Status == (int)AVCStatus.Yellow);
var q = from a in odc.AVCs
let Y = (from p in yellows.Where(o => o.AVCId == a.Id) select p).Any()
where Y == true
group a by new { a.Id, a.Name, a.Address } into agroup
select new AVCListItem
{
Id = agroup.Key.Id,
Name = agroup.Key.Name,
Address = agroup.Key.Address,
Count = agroup.Count(o => o.Id != null)
};
You don't need the join, nor the grouping:
var q = from a in odc.AVCs
select new AVCListItem
{
Id = a.Id,
Name = a.Name,
Address = a.Address,
Count = yellows.Where(o => o.AVCId == a.Id).Count()
};
I am getting data from multiple tables by joining and i want to group data on particular column value but after group by statement i can access my aliases and their properties. What mistake i am making?
public List<PatientHistory> GetPatientHistory(long prid)
{
using(var db = new bc_limsEntities())
{
List<PatientHistory> result =
(from r in db.dc_tresult
join t in db.dc_tp_test on r.testid equals t.TestId into x
from t in x.DefaultIfEmpty()
join a in db.dc_tp_attributes on r.attributeid equals a.AttributeId into y
from a in y.DefaultIfEmpty()
where r.prid == prid
group new {r,t,a} by new {r.testid} into g
select new PatientHistory
{
resultid = r.resultid,
bookingid = r.bookingid,
testid = r.testid,
prid = r.prid,
attributeid = r.attributeid,
result = r.result,
Test_Name = t.Test_Name,
Attribute_Name = a.Attribute_Name,
enteredon = r.enteredon,
Attribute_Type = a.Attribute_Type
}).ToList();
return result;
}
}
You're doing this wrong way. As been said by Jon after grouping the sequences with aliases r,t,a doesn't exist. After grouping you receive the sequence g with sequances of r,t,a in each element of g. If you want get one object from each group (for example most recent) you should try this:
List<PatientHistory> result =
(from r in db.dc_tresult
join t in db.dc_tp_test on r.testid equals t.TestId into x
from t in x.DefaultIfEmpty()
join a in db.dc_tp_attributes on r.attributeid equals a.AttributeId into y
from a in y.DefaultIfEmpty()
where r.prid == prid
group new {r,t,a} by new {r.testid} into g
select new PatientHistory
{
resultid = g.Select(x => x.r.resultid).Last(), // if you expect single value get it with Single()
// .... here add the rest properties
Attribute_Type = g.Select(x => x.a.Attribute_Type).Last()
}).ToList();
I appreciated this question so I thought I would add another potential usage case. I would like feedback on what the cleanest approach is to getting table information through a group operation so that I can project later in the select operation. I ended up combining what the OP did which is to pass objects into his group clause and then used the g.Select approach suggested by YD1m to get table information out later. I have a LEFT JOIN so I'm defending against nulls :
// SQL Query
//DECLARE #idCamp as Integer = 1
//
//select *,
//(select
//count(idActivityMaster)
//FROM tbActivityMasters
//WHERE dftidActivityCategory = A.idActivityCategory) as masterCount
//FROM tbactivitycategories A
//WHERE idcamp = #idCamp
//ORDER BY CategoryName
int idCamp = 1;
var desiredResult =
(from c in tbActivityCategories
.Where(w => w.idCamp == idCamp)
from m in tbActivityMasters
.Where(m => m.dftidActivityCategory == c.idActivityCategory)
.DefaultIfEmpty() // LEFT OUTER JOIN
where c.idCamp == idCamp
group new {c, m} by new { m.dftidActivityCategory } into g
select new
{
idActivityCategory = g.Select(x => x.m == null ? 0 : x.m.dftidActivityCategory).First(),
idCamp = g.Select(x => x.c.idCamp).First(),
CategoryName = g.Select(x => x.c.CategoryName).First(),
CategoryDescription = g.Select(x => x.c.CategoryDescription).First(),
masterCount = g.Count(x => x.m != null)
}).OrderBy(o=> o.idActivityCategory);
desiredResult.Dump("desiredResult");
If I just use a basic group approach I get the results but not the extra column information. At least I can't find it once I group.
var simpleGroup = (from c in tbActivityCategories
.Where(w => w.idCamp == idCamp)
.OrderBy(o => o.CategoryName)
from m in tbActivityMasters
.Where(m => m.dftidActivityCategory == c.idActivityCategory)
.DefaultIfEmpty() // LEFT OUTER JOIN
where c.idCamp == idCamp
group m by m == null ? 0 : m.dftidActivityCategory into g
select new
{
// How do I best get the extra desired column information from other tables that I had before grouping
// but still have the benefit of the grouping?
// idActivityCategory = g.Select(x => x.m == null ? 0 : x.m.dftidActivityCategory).First(),
// idCamp = g.Select(x => x.c.idCamp).First(),
// CategoryName = g.Select(x => x.c.CategoryName).First(),
// CategoryDescription = g.Select(x => x.c.CategoryDescription).First(),
// masterCount = g.Count(x => x.m != null)
idActivityCategory = g.Key,
masterCount = g.Count(x => x != null)
});
simpleGroup.Dump("simpleGroup");
Please tear this up. I'm trying to learn and it just seems like I'm missing the big picture here. Thanks.
UPDATE : Cleaned up by moving the work into the group and making the select more straight forward. If I had known this yesterday then this would have been my original answer to the OP question.
int idCamp = 1;
var desiredResult =
(from c in tbActivityCategories
.Where(w => w.idCamp == idCamp)
from m in tbActivityMasters
.Where(m => m.dftidActivityCategory == c.idActivityCategory)
.DefaultIfEmpty() // LEFT OUTER JOIN
where c.idCamp == idCamp
group new { c, m } by new
{ idActivityCategory = m == null ? 0 : m.dftidActivityCategory,
idCamp = c.idCamp,
CateGoryName = c.CategoryName,
CategoryDescription = c.CategoryDescription
} into g
select new
{
idActivityCategory = g.Key.idActivityCategory,
idCamp = g.Key.idCamp,
CategoryName = g.Key.CateGoryName,
CategoryDescription = g.Key.CategoryDescription,
masterCount = g.Count(x => x.m != null)
}).OrderBy(o => o.idActivityCategory);
desiredResult.Dump("desiredResult");