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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:27:20+00:00 2026-05-26T09:27:20+00:00

I am trying to convert a complex (and rather hacky) dynamic SQL query in

  • 0

I am trying to convert a complex (and rather hacky) dynamic SQL query in to a LINQ query.

I have the following LINQ query so far:

var results = (
    from c in Customers
    from d in MonthCalendar
        join f in Facilities on c.CustomerCode equals f.CustomerCode
        join p in ProjectedCashFlows on f.FacilityId equals p.FacilityId into pj
                from p in pj.DefaultIfEmpty()                      
        where d.ExpectedYear == currentYear 
                && f.FacilityStatusId == 1
                && (p.ExpectedYear == null || d.ExpectedYear == p.ExpectedYear)
                && (p.ExpectedMonth == null || d.ExpectedMonth == p.ExpectedMonth)
                && c.PrimaryArmId == userId 
                && (p.ProjectedCashFlowStatusId == null || p.ProjectedCashFlowStatusId != 4)
        select new 
            {
                CustomerCode = c.CustomerCode,
                CustomerName = c.CustomerName,
                FacilityId = f.FacilityId,
                FacilityDescription = f.FacilityProductDescription,
                FacilityCurrency = f.FacilityCurrencyId,
                FacilityLimit = f.Limit,
                ExpectedYear = d.ExpectedYear,
                ExpectedMonth = d.ExpectedMonth,
                ExpectedAmount = p == null ? 0 : (double)p.ExpectedAmount

            }
            );

I am trying to retrieve details from a Customer table that has a one-to-many relationship with a Facilities table. I am then trying to retrieve any details located in the ProjectedCashFlows

The problem I am having is that the query should return all Customer and Facilites information regardless of whether any values exist in the ProjectedCashFlows table.

Unfortunately this query is not doing that – it is only returning Customer and Facilities information when the Facility exists in the ProjectedCashFlows table.

I have used a MonthCalender table to list out each month in the year.

The relevant table information is:

Customers

  • CustomerCode
  • CustomerName
  • PrimaryArmId

Facilities

  • CustomerCode
  • FacilityId
  • FacilityCurrencyId
  • FaciliyLimit
  • FacilityDescription

ProjectedCashFlows

  • CustomerCode
  • FacilityId
  • ExpectedYear
  • ExpectedMonth
  • ExpectedAmount
  • ProjectedCashFlowStatusId

MonthsCalendar

  • ExpectedMonth
  • ExpectedYear

As an example I have a customer that has 4 rows in the Facilities table however, 2 of these facilities do not appear in the ProjectedCashFlows table so they are not being displayed.

If an entry doesn’t exist in ProjectedCashFlows it should take the ExpectedMonth & ExpectedYear from the CalendarMonths table, return 0 for the ExpectedAmount and use the FacilityId from the Facilities table.

As you can probably work out I have just started to use LINQ.

Can anyone poke me in the right direction?

  • 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-26T09:27:20+00:00Added an answer on May 26, 2026 at 9:27 am

    Your query uses p assuming it is non-null:

    where d.ExpectedYear == currentYear 
          && f.FacilityStatusId == 1
          && (p.ExpectedYear == null || d.ExpectedYear == p.ExpectedYear)
          // etc
    

    But you’ve used DefaultIfEmpty() which will logically create a sequence with a single null value when there are no ProjectedCashFlows.

    So basically you need something like:

    where d.ExpectedYear == currentYear 
          && f.FacilityStatusId == 1
          && (p == null ||
              ((p.ExpectedYear == null || d.ExpectedYear == p.ExpectedYear)
              // etc
              ))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to convert a complex SELECT query to an UPDATE query,
I'm trying to find a way to convert this very complex SQL Query into
How to do an update in LINQ to Objects. Trying convert SQL to Linq
Trying to convert the following but am having difficulty. Can anyone see where I'm
I'm trying to convert an incoming sting of 1s and 0s from stdin into
I am trying to pass data from SQL, to C#, then to an R
So I have a SQL Query as Follows SELECT P.Date, P.CategoryName, P.ProductName, SUM(Quantity) Quantity,
I am trying to convert dips to pixels using the following function. I’ve tried
I'm trying convert xml from one format to other using the XslCompiledTransform in c#.
I'm trying to convert a complex Java applet to JavaScript. The applet does all

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.