score:2

Accepted answer

This should be a straightforward translation of the SQL:

from s in db.tblSearches
where
    s.seaPrice >= 2000 &&
    s.seaPrice <= 7000
group s by new {s.seaMake, s.seaModel} into g
orderby g.Key
select new
{
    Make =  g.Key.seaMake,
    Model = g.Key.seaModel,
    Count = g.Count(),
    From =  g.Min(x => x.seaPrice),
    CapId = g.Min(x => x.seaCapId)
}

score:1

Instead of your original collection of IEnumerable<TypeOfS> when you grouped into g you converted that collection into an IEnumerable> so the collection in current scope is g. So the following would be valid

from s in db.tblSearches
where s.seaPrice >= 2000
   && s.seaPrice <= 7000
orderby s.seaMake
group s by s.seaMake into g // the collection is now IEnumerable<IGrouping<TypeOfSeaMake, TypeofS>>
select new {
    make = g.Key, // this was populated by s.seaMake
    model = g.First().seaModel, // get the first item in the collection
    count = g.Max(x => x.seaMake), // get the max value from the collection
    PriceFrom = g.Min(x => x.seaPrice), // get the min price from the collection
};

there will now be one item returned for each grouping


Related Articles