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?
Related
I have the following LINQ
Dim z = (From d In db.GPSdevice
Where d.CompanyId = currentuser.CompanyId And d.Type = "Truck" Or d.Type = "Trailer"
Order By d.ListOrder Descending
Group d By d.Driver Into g = Group
Select g.FirstOrDefault())
I try to convert it to c#
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => new { Group = g });
but not sure, how to convert Select g.FirstOrDefault()...
You can use the query syntax in C# too, no need to rewrite using the extension methods directly:
var z = (from d In db.GPSdevice
where (d.CompanyId == currentuser.CompanyId) && (d.Type == "Truck") || (d.Type == "Trailer")
orderby d.ListOrder descending
group d by d.Driver into g = group
select g.FirstOrDefault())
Just call g.FirstOrDefault() in your Select
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => g.FirstOrDefault());
I've been puzzling over this problem all morning and can't figure out how to do it in C#.
My SQL query as follows:
select a.CourseID,
a.UserID
from audit a
inner join results r on a.UserID = r.UserID
inner join Course c on a.CourseID = c.CourseID
where c.CourseType = 9 and a.Guid = 'A123F123D123AS123123'
and a.Result = 'Passed' and r.Class = 'Maths'
group by a.CourseID, a.UserID
order by a.UserID
returns exactly what I want, but I can't seem to translate it into linq format. (the format being used here is what is required in my job at the moment so please advise on this format)
So far I have the following:
var audits = auditRepository.Get(a => a.Course.CourseType == 9 && a.GUID == this.Company.GUID && a.Result == "Passed", null, null,
a => a.Course, a => a.User)
.Join(resultsRepository.Get(r => r.GUID == this.Company.GUID && r.Class == class),
a => a.UserID,
r => r.UserID,
(a, r) => new Audit
{
User = a.User,
Course = a.Course,
Result = a.Result,
Timestamp = a.Timestamp,
AuditID = a.AuditID,
UserID = a.UserID
}
)
.OrderByDescending(o => o.Timestamp)
.GroupBy(u => new { u.User, u.Course })
.Select(grp => grp.ToList())
.ToList();
However this returns duplicates.
Any advice is appreciated, thanks
H
Instead of
.Select(grp => grp.ToList())
Select only the first element from each group to exclude duplicates:
.Select(grp => grp.First())
If you need a count also:
.Select(t => new{grp = t.First(),cnt = t.Count()} )
Fix:
.Select(t => new { grp = t.First(), cnt = t.Select(s => s.AuditID).Distinct().Count() })
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
Consider the following LINQ statement:
var posts = db.Posts
.Where(p => p.Votes.Count > 0 && p.User.Confirmed)
.Select(p => new
{
PostId = p.PostId,
Votes = p.Votes.Count(),
Hours = EntityFunctions.DiffHours(DateTime.UtcNow, p.Timestamp)
})
.Select(p1 => new
{
PostId = p1.PostId,
Votes = p1.Votes,
Group = p1.Hours <= 24 ? 24 :
p1.Hours <= 168 ? 168 :
p1.Hours <= 720 ? 720 : 0
})
.Where(p2 => p2.Group != 0);
It successfully groups a listing of posts into their respective groups: 24 hours, 168 hours, and 720 hours.
However, now I need to get the PostId that has the Max Votes for each group. How do I do that?
var postIds = posts.OrderByDescending(x => x.PostId).GroupBy(x => x.Group)
.Select(x => x.First().PostId);
Or, for a bit more clarity (IMHO), and (I think) less speed:
var postIds = posts.GroupBy(x => x.Group).Select(g => g.Max(p => p.PostId));
The former has the benefit that if you want the post, and not just the PostId, you have that available more easily.
I was looking at this but kind of slow. It's a little different syntax so I'll post it anyway
var groups = (from p in posts
group p by p.Group into g
select new
{
Id = g.Max(p => p.Id),
Group = g.Key
}).ToList();
var bestPosts = (from p in posts
join j in groups on new {p.Group, p.Votes} equals new {j.Group, j.Votes}
select p).ToList();
Groups according to "GroupByField" and selects the max.
var query = from o in _context.Objects
group o by o.GroupByField
into group
select new
{
maxParameter = (from o in group orderby o.OrderByField select o).Last()
};
and then in order to select the original (max) objects
var largest = query.Select(q => q.maxParameter).ToList();
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");