score:1
try avoiding the
context.procedures.where(h => h.id == x.key).select(h => h.code).firstordefault()
by including the code
in the groupby
clause - i know it seems redundant, but ef is known to have problems translating operations on grouping that involve something other than using key accessors and aggregates:
//...
.groupby(x => new { id = x.visitline.procedureid, x.visitline.procedure.code })
.select(x => new
{
id = x.key.id,
paidamount = x.sum(t => t.paidamount),
code = x.key.code
}).toarray();
update: the above generates the following sql in my test environment (latest ef6.1.3):
select
1 as [c1],
[groupby1].[k1] as [procedureid],
[groupby1].[a1] as [c2],
[groupby1].[k2] as [code]
from ( select
[extent5].[procedureid] as [k1],
[extent6].[code] as [k2],
sum([filter1].[paidamount]) as [a1]
from (select [extent1].[visitlineid] as [visitlineid], [extent1].[paidamount] as [paidamount]
from [dbo].[transactionline] as [extent1]
inner join [dbo].[transaction] as [extent2] on [extent1].[transactionid] = [extent2].[id]
left outer join [dbo].[eob] as [extent3] on [extent2].[eobid] = [extent3].[id]
left outer join [dbo].[eobbatch] as [extent4] on [extent3].[eobbatchid] = [extent4].[id]
where (1433 = [extent2].[transactiontypeid]) and ([extent3].[eobbatchid] is null or [extent4].[status] = 1) ) as [filter1]
left outer join [dbo].[visitline] as [extent5] on [filter1].[visitlineid] = [extent5].[id]
left outer join [dbo].[procedure] as [extent6] on [extent5].[procedureid] = [extent6].[id]
group by [extent5].[procedureid], [extent6].[code]
) as [groupby1]
which is much better as i expected.
update 2: ef is a strange beast. using double projection produces the desired result:
//...
.groupby(x => x.visitline.procedureid)
.select(x => new
{
id = x.key,
paidamount = x.sum(t => t.paidamount),
})
.select(x => new
{
x.id,
x.paidamount,
code = context.procedures.where(h => h.id == x.id).select(h => h.code).firstordefault()
}).toarray();
which produces the following:
select
1 as [c1],
[project2].[procedureid] as [procedureid],
[project2].[c1] as [c2],
[project2].[c2] as [c3]
from ( select
[groupby1].[a1] as [c1],
[groupby1].[k1] as [procedureid],
(select top (1)
[extent6].[code] as [code]
from [dbo].[procedure] as [extent6]
where [extent6].[id] = [groupby1].[k1]) as [c2]
from ( select
[extent5].[procedureid] as [k1],
sum([filter1].[paidamount]) as [a1]
from (select [extent1].[visitlineid] as [visitlineid], [extent1].[paidamount] as [paidamount]
from [dbo].[transactionline] as [extent1]
inner join [dbo].[transaction] as [extent2] on [extent1].[transactionid] = [extent2].[id]
left outer join [dbo].[eob] as [extent3] on [extent2].[eobid] = [extent3].[id]
left outer join [dbo].[eobbatch] as [extent4] on [extent3].[eobbatchid] = [extent4].[id]
where (1433 = [extent2].[transactiontypeid]) and ([extent3].[eobbatchid] is null or [extent4].[status] = 1) ) as [filter1]
left outer join [dbo].[visitline] as [extent5] on [filter1].[visitlineid] = [extent5].[id]
group by [extent5].[procedureid]
) as [groupby1]
) as [project2]
p.s. if it's unclear, the answer to your concrete question
is it possible to force ef to generate sql exactly as it was written within linq ?
is no. contrary, you should write linq query in a certain way in order to get the desired (or closer) sql query.
Source: stackoverflow.com
Related Query
- Entity Framework GroupBy to Sql Generation
- SQL subquery result in LINQ and Entity Framework Code First
- Entity Framework Code First ToList method timing out on SQL Azure
- Equivalent sql code insert/sql in entity framework in code-first
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- Why does the Entity Framework generate nested SQL queries?
- Entity Framework 6 Code First Custom Functions
- Entity Framework filter data by string sql
- Entity Framework & LINQ To SQL - Conflict of interest?
- Get All Except from SQL database using Entity Framework
- Entity Framework GroupBy take the oldest with mySQL
- How can i write SQL update query with where clause in Entity Framework in C#
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Entity Framework generates inefficient SQL for paged query
- Why does Entity Framework 6 generate complex SQL queries for simple lookups?
- Avoiding repeated projection code in Entity Framework
- Forcing Entity Framework to not generate NCLOB's when building Linq-to-Sql Code (Model First)
- Linq to SQL and Entity Framework differences?
- Entity Framework gets progressively slow with extra join added even though SQL generated is fast
- Different results in Entity Framework than LINQ to SQL
- Entity Framework Code First without app.config
- Optimize SQL generated by LINQ Query in Entity Framework 4.1 with one-to-many associations
- Entity Framework 5 and SQL Queries
- Query Xml from SQL using Entity Framework Database First
- ASP NET CORE Entity Framework Select with GroupBy Id
- How do LINQ queries against the Entity Framework communicate dates to a SQL Server?
- How to get Entity Framework 6 to use SQL STUFF function inside CSDL?
- Entity Framework Code First using context in Controller
- SQL Server Profiler do not trace all Entity Framework 4 queries - issue
- How to optimize SQL query generated by Entity Framework in SQL Server Management Studio?
More Query from same tag
- What is the use of Enumerable.Zip extension method in Linq?
- SelectMany Expression Tree Expression.Call typeArguments
- How to fix this ArrayIndex error?
- Many-to-many relations in entity framework causes infinite loop
- Recursive Linq - Parent indication for child property?
- How do i do the following using LINQ feature of C#?
- compare two folders for non identical files with SymmetricDifference?
- Modify a LINQ `InRange` extension method to act as `NotInRange`
- Linq-OrderByDescending doesn't work for me-ASP.net MVC
- What's better for creating distinct data structures: HashSet or Linq's Distinct()?
- How to make from two methods one method in a repository
- LINQ make All method async
- how read in foreach linq two level query en
- LINQ to XML question
- PLINQ + LINQ = NRE?
- Dynamic LINQ Multiple Where Clause
- Error - LINQ to Entities does not recognize the method 'System.String ToString(System.IFormatProvider)'
- Get Duplicates in Range of Numbers
- linq, selecting columns as IEnumerable<DataRow>
- How much space taken when using System.Linq?
- Compounded Null Checking With Non-Nullable Parameter
- LINQ to Entities three table join query
- Linq and C#, Skipping records, casting error
- Databinding to repeate dynamic object creation
- How can I get a string that contains all the selected items in a multiselectlist?
- Add the where clause dynamically in Entity Framework
- What's wrong with my EF query?
- How to convert a ISingleResult<T> containing a string to a string array
- Linq remove only one item if there are duplicate
- The entity or complex type 'WebApi.DbConnection.LoginCrediential' cannot be constructed in a LINQ to Entities query