0

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)

3 Answers 3

4

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 .

2
  • All answers contains lambda expressions which I'm not really used to. Can you explain whats going on here? It's not working when I'm trying it out with LINQPad 4. 'LINQPad.User.Ad' does not contain a definition for 'FeatureID' and no extension method 'FeatureID' accepting a first argument of type 'LINQPad.User.Ad' could be found (press F4 to add a using directive or assembly reference) Oct 17, 2013 at 11:54
  • 1
    Worked with some modifications (updated in original post). Thanks! Oct 17, 2013 at 12:18
0

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);
0

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?

1
  • The grouping is to get the minimum value per company. Your query gets the minimum value globally, so it could even be 0. Oct 17, 2013 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.