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
Source: stackoverflow.com
Related Query
- LINQ Query with Aggregates and Group By
- Entity Framework - Linq query with order by and group by
- LINQ Query with GROUP and SUM
- Simple sql to Linq query with group by and aggregate functions
- Translating query with GROUP BY and COUNT to Linq
- LINQ query with GROUP BY and Count(*) into Anonymous Type
- Optimize/rewrite LINQ query with GROUP BY and COUNT
- Linq query with GROUP BY on multiple fields, SUM and COUNT
- LINQ Dynamic Query with group by and string variable
- Group by year and month with given date range using Lambda or LINQ Query
- How to write a count and group by month query with linq
- LINQ with sub select query using sum and group by?
- Linq query with left join and group
- How to group and return Column1 with total number of "true" in boolean Column2 Linq query and display in View?
- Convert Sql query with Group by and Having clause to Linq To Sql query in C#
- Linq group by query with count and other fields in mvc4
- MVC Linq query with both group by columns and Max table column
- Linq query to show grid of results with group by and count
- LINQ query with group joins and count needs refactoring
- use linq to group by and then run select query with where condition
- Sql linq query with where and group by
- LINQ to Entities query for count and group by with join on 2 tables
- Converting an SQL with Left Join , Group and Sum to a Linq Query
- SQL to C# LINQ query with multiple group by inner queries and having clauses
- Group By and Sum In LINQ query with Join
- Linq query with group by and max date, get Id
- Group By Having and Count as LINQ query with multiply nested tables
- EF Linq query with join, group and sum
- LINQ version of sql query with multiple left joins, group by, and datetime conversion
- C# LINQ Query with group by, having and where clause invalid
More Query from same tag
- Linq to SQL multiple conditional where clauses
- Populate list using LINQ
- "And" condition in C#/LINQ Query
- Windows Phone 8 Debug local database records with references doesn't update after restart
- Natural sort filenames with leading numbers in List(Of IO.FileInfo)?
- Error when calling another repo - 'There is already an open DataReader associated with this Command which must be closed first'
- linq create index with select
- How to handle System.ArgumentOutOfRangeException error when getting values from JSON by Property Name
- Check whether a particular node exist in xml data
- LINQ select from table where fieldValue is in another tables list of values
- How to convert SQL query to LINQ with Orderby, Groupby
- Why does IEnumerable.ToList produce new objects
- LINQ Left Join 3 tables with OR operator
- How to calculate an ETA for a LINQ pipeline to complete?
- Linq to return list with calculated sums based on property
- Lock a single access variable for parallel threads in C#
- get most frequently used values with linq
- Linq Key that contains only the first of each duplicate
- OrganizationServiceContext.CreateQuery vs Fetch
- How to populate an array from a DataSet in VB.NET
- Java/Android best equivalent for C# linq
- List of class objects - Filter class objects with same values using LINQ
- It is possible to use LINQ in HtmlAgilityPack in C#?
- Linq Distinct does not function as per forum and Microsoft examples
- LinQ Aggregate Error - Sequence contains no elements
- Iterate over a list that contains a self-referencing list
- Use or in entity framework
- 'let' in Linq Statement generates Cross Join
- How to use distinct with group by in Linq to SQL
- C# EF Linq bitwise question