Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:28:38+00:00 2026-05-23T04:28:38+00:00

How do I convert the following query into a Linq expression? SELECT p.playerid, (SELECT

  • 0

How do I convert the following query into a Linq expression?

SELECT p.playerid,
       (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) AS runsfor,
       (SELECT SUM(d.runs)
        FROM   deliveries d
               INNER JOIN overs o
                 ON d.overid = o.overid
        WHERE  o.isbatting = 0
               AND o.gameid = 5
               AND d.player_playerid = playerid) AS runsagainst,
       ( (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) -
         (SELECT SUM(d.runs)
          FROM   deliveries d
                 INNER JOIN overs o ON d.overid = o.overid
          WHERE  o.isbatting = 0
                 AND o.gameid = 5
                 AND d.player_playerid =
                     playerid) ) AS runscontributed
FROM   deliveries d
       INNER JOIN players p ON d.player_playerid = p.playerid
       INNER JOIN overs o   ON d.overid = o.overid
WHERE  o.gameid = 1
GROUP  BY p.playerid  

The results generated look like:

2   13  16  -3
4   -5  18  -23
5   -6  11  -17
7   4   1   3
8   5   7   -2
9   12  17  -5
10  -4  24  -28
12  19  1   18
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:28:39+00:00Added an answer on May 23, 2026 at 4:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone hep me converting the following SQL query into LINQ? select convert(varchar(10),date,110) as
How do I convert the following query into linq to sql query? select invoice.id,
How do I convert the following SQL statement into Lambda Expression or Linq Query?
Can someone please help convert the following into LINQ To SQL query? select *
I am trying to convert the following SQL into NHibernate: SELECT * FROM dbo.Customer
I would like to convert the following query: SELECT request.requestId FROM request LEFT OUTER
How can I convert the following SQL queries into LINQ query form in C#,
I have the following C# LINQ query expression: var duplicatedSSN = from p in
I want to convert the following SQL Query to a SubSonic Query. SELECT [dbo].[tbl_Agency].[ParentCompanyID]
I am attempting to convert the following HQL query to Criteria. from SubportfolioAudit as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.