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 8227429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:07:40+00:00 2026-06-07T16:07:40+00:00

I have a linq to sql query that returns some orders with non zero

  • 0

I have a linq to sql query that returns some orders with non zero balance (in fact, the query is a little bit complicated, but for simplicity I omitted some details). This query also should return orders with no CardItems (both sub-queries return NULL in T-SQL, and comparing two NULLS gives FALSE, so I convert NULL result values of sub-queries to 0 for comparing).

var q = (from o in db.Orders
         where db.Cards(p =>
             p.OrderId == o.Id 
             && p.Sum + (db.CardItems.Where(i => i.IncomeId == p.Id)
                        .Sum(i => (double?)i.Amount) ?? 0) 
                    != (db.CardItems.Where(i => i.DeductId == p.Id)
                        .Sum(i => (double?)i.Amount) ?? 0)
            ).Any()
         select o);

The problem is, that converting expression Sum(i => (double?)i.Amount) ?? 0 produce COALESCE operator, which is ten times slower than exactly the same T-SQL query with replaced COALESCE to ISNULL because of sub-query in it. Is there any possibility to generate ISNULL in this situation?

  • 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-06-07T16:07:41+00:00Added an answer on June 7, 2026 at 4:07 pm

    In my experience, coaxing linq into generating the SQL you want is a hassle at best. If you’ve got an query implementation that’s better than linq (works correctly and performs well), go ahead and use it, even if just for this one query.

    The quickest way is probably with DataContext.ExecuteQuery<TResult>. It will even hydrate and handle the returned objects for you, just as if linq generated the query itself.

    http://msdn.microsoft.com/en-us/library/bb361109.aspx

    Many people would rather put this SQL in a stored procedure, especially in production code. Then you just have to map the stored procedure into your linq objects, and it works the same way. As is often the case, ScottGu has a rather detailed post about how to do that:

    http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

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

Sidebar

Related Questions

I have a very complex Linq to SQL query that returns a result set
I have a LINQ from SQL query that returns sorted rows. For instance, it
I have a linq to sql query that produces duplicates. i can seem to
I have some linq to sql method and when it does the query it
I have a LINQ-to-SQL query that runs through a table, that I want to
I have a very simple linq to sql query in C#: int acctNum =
I have a DataGridView bound to a LINQ to SQL query expression. I want
I have a LINQ TO SQL Context that I have created that calls a
I have a LINQ to SQL DataContext that queries four different tables. But I
I I have a linq-to-sql database in which I want to log some of

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.