score:4
Accepted answer
start out by simplifying.
select d.*, o.*
from deliveries d
inner join overs o on d.overid = o.overid
var joined = from d in deliveries
join o in overs on d.overid equals o.overid
select new { d, o };
then simplify some more...
select sum(d.runs)
from deliveries d
inner join overs o on d.overid = o.overid
where o.isbatting = 1
and o.gameid = 5
and d.player_playerid = playerid
(from j in joined
where j.o.isbatting
&& j.o.gameid == 5
&& j.d.player.playerid == playerid
select j.d.runs).sum();
lather, rinse, repeat:
var joined = from d in deliveries
join players p on d.player_playerid equals p.playerid
join o in overs on d.overid equals o.overid;
where j.o.gameid = 1
select new { p, d, o };
var _runsfor = from j in joined
where j.o.isbatting
&& j.o.gameid == 5
&& j.d.player.playerid == some_player_id
select j;
var ungrouped = from j in joined
select new
{
playerid = j.p.playerid,
runsfor = _runsfor.where(r => r.p.playerid == j.p.playerid)
.sum(jn => jn.d.runs),
runsagainst = //etc...
};
var grouped = from u in ungrouped
group new { u.runsfor, u.runsagainst, /* etc... */ }
by u.playerid into player
select player;
i'm not certain this will do what you want, but it should give you a jumping off point.
don't use this code directly; i wrote it freehand, and i'm not sure it will work the first time without some tweaking. the real point here is to simplify. break your sql query into smaller groups and write the linq for those. then write some more linq to tie it all together. the ability to do that is one of the best things about linq.
Source: stackoverflow.com
Related Query
- How to convert this complex SQL Query with Subqueries into LINQ
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How do I convert this SQL inner join query into LINQ syntax?
- How to convert this sql query into linq query. I am stuck at the group by and sum part
- How to convert this query string into LINQ to SQL query?
- How to get SQL query into LINQ form in C# code
- How do I get this Linq query with custom join into a DataSet?
- How do I convert this SQL query to LINQ to SQL
- How to convert this SQL query to LINQ or Lambda expression?
- How is this Nested Set SQL query converted into a LINQ query?
- How do i convert this linq code to inline sql
- How to convert SQL query to Linq with GROUP BY on only date part of a datetime?
- How to convert this String-based sql query to use Linq
- How to make this SQL query into DotNet Core 2.1 Linq
- Convert a sql query with a subquery into a linq statement
- How to convert sql inner join query into linq to sql query and convert into list
- How to rewrite this LINQ query with SQL syntax and <>?
- How to convert SQL to linq for this query
- How can I convert Sql query to Linq and its equivalent with Join() method in Entity Framework Core
- How to Convert this Linq Query into Lambda Expression
- How can I convert this SQL query to LINQ
- How to convert this SQL query to Linq
- Can anyone convert this SQL query into a C# Linq statement?
- How can i convert this code snippet into LINQ format?
- Convert this SQL query into Linq
- How to convert this SQL query to LINQ or Lambda expression in C#?
- How do I convert SQL Query with Ccase Switch to Linq Lambda
- How to convert this query from SQL to LINQ
- How to make this simple SQL query into a Linq Statement?
- Converting SQL query into Linq which used complex sum with group by
More Query from same tag
- Numbers of iteration generated in a single LINQ query
- Linq union two tables where on has an extra column
- How can I write my SQL query in LINQ?
- Should I use LINQ for this query?
- LINQ Multiple table left join, distinct count not giving proper result
- Varied results on Linq list select
- LINQ sort a flat list based on childorder
- Query or algorithm to generate a structure to populate a tree (C#, LINQ)
- C# Linq Joining with AND conditions
- LINQ compare 2 tables, only return previous item, not all
- Group items in a list based on two properties using LINQ MVC
- Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?
- Asp.net mvc Linq query for multiple filters
- LINQ Select child records error "The query contains a projection"
- Data Field Value Equivalents
- linq query to find distinct combinations of list items taking 2 at a time c#
- converting LINQ group by result straight into model
- SelectMany Expression Tree Expression.Call typeArguments
- Linq query-dateOfBirth to age
- How can I make LINQ Lambda expressions fail gracefully like XPath?
- Moving specific elements after others but not to top or bottom
- Reflection + Linq to get all properties that has Attribute in Assembly
- Optimize or ditch LINQ query?
- Searching a String in a Dictionary
- How to get Entity Framework 6 to use SQL STUFF function inside CSDL?
- How can i do this with Extensions methods or Linq?
- Using LINQ to SQL where database tables can have extra columns
- Getting this exception: "DbSortClause expressions must have a type that is order comparable. Parameter name: key"
- Implement in operator in Entity Framework
- C# extracting ID from list of structures using LINQ