Note that there are some explanatory texts on larger screens.

plurals
  1. POJoining queries with Entity Framework
    primarykey
    data
    text
    <p>I have the table</p> <p><strong>MOVIES_RATING:</strong> </p> <ul> <li><code>MovieID int</code></li> <li><code>MovieRating decimal</code></li> </ul> <p>I'd like to get 2 values using one query:</p> <pre><code>select COUNT(*) FROM [dbo].[MOVIES_RATING] where [dbo].[MOVIES_RATING].[MovieID] = 78 </code></pre> <p>and</p> <pre><code>select SUM([dbo].[MOVIES_RATING].Rating) FROM [dbo].[MOVIES_RATING] where [dbo].[MOVIES_RATING].[MovieID] = 78 </code></pre> <p>That's what I got in LINQ:</p> <pre><code>(from p in ef.MOVIES_RATING.Where(r =&gt; r.MovieID== movie_id) let movieRates = ef.MOVIES_RATING.Where(r =&gt; r.MovieID == movie_id) let count = movieRates.Count() let averageUserRating = movieRates.Sum(c =&gt; c.MOVIES_RATING)/count select new MovieRating {AverageUserRating = averageUserRating, VoteCount = count}) .Take(1); </code></pre> <p>Looks awful as well as SQL being generated:</p> <pre><code>SELECT [Limit1].[MovieID] AS [MovieID], [Limit1].[C2] AS [C1], [Limit1].[C1] AS [C2] FROM ( SELECT TOP 1 [GroupBy1].[A1] AS [C1], [Extent1].[MovieID] AS [MovieID], [GroupBy2].[A1] / CAST( [GroupBy1].[A1] AS decimal(19,0)) AS [C2] FROM [dbo].[MOVIES_RATING] AS [Extent1] CROSS JOIN (SELECT COUNT(1) AS [A1] FROM [dbo].[MOVIES_RATING] AS [Extent2] WHERE [Extent2].[MovieID] = 78 ) AS [GroupBy1] CROSS JOIN (SELECT SUM([Extent3].[Rating]) AS [A1] FROM [dbo].[MOVIES_RATING] AS [Extent3] WHERE [Extent3].[MovieID] = 78 ) AS [GroupBy2] WHERE [Extent1].[MovieID] = 78 ) AS [Limit1] </code></pre> <p>I'm not sure it is best solution, so any help is appreciated.</p> <p>I know It can be done using stored procedure, but if its could be done using LINQ it would be better.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload