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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:15:46+00:00 2026-06-09T23:15:46+00:00

I have the following join statement: var query = from holding in dHoldList join

  • 0

I have the following join statement:

  var query = from holding in dHoldList
              join client in clientList on
                new { holding.ClientNo }
                equals new { client.ClientNo } into clj
              from cl in clj.DefaultIfEmpty()
              join matter in matterList on 
                new { holding.ClientNo } 
                equals new { matter.ClientNo } into mlj
              from ml in mlj.DefaultIfEmpty()
              join stk in stockList on
                new { holding.Sedol }
                equals new { stk.Sedol } into slj
              from sl in slj.DefaultIfEmpty()
              select
                new GeneralHoldingsReport()
                {
                  ClientNo = holding.ClientNo,
                  Depot = holding.Depot,
                  HoldingSedol = holding.Sedol,
                  Value = holding.ValueOfStock,
                  NoOfUnits = holding.QuantityHeld,
                  ClientName = (cl == null ? null : cl.ClientName),
                  CountryOfResidence = (cl == null ? null : cl.CountryOfResidence),
                  BG = (cl == null ? null : cl.BusinessGetter),
                  ClientStockValue = (ml == null ? 0 : ml.FullValueOfPortfolio),
                  StockName = (sl == null ? null : sl.R1.Trim() + " " + sl.R2.Trim())
                };


  var reportList = query.ToList();

However when it runs I am getting an out of memory exception error.

I require the dHoldList to be the main table, with all other tables being left joins into it (i.e. if data is matched in other tables relating to each record in dHoldList then return data, if not just blank.) I believe I am doing this correctly, but obviously not.

Each of these lists contains approx 300k lines, apart from the client which is only 30k, so that may be causing some of the issues here.

  • 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-09T23:15:47+00:00Added an answer on June 9, 2026 at 11:15 pm
    1. You are instantiating a LOT of useless anonymous types in your joins. You can write them as follows:

      join client in clientList on holding.ClientNo equals client.ClientNo into clj 
      

      Maybe this already solves your memory problem?
      This does only apply to LINQ to Objects. If your query is translated into SQL as is the case with EF or LINQ to SQL these anonymous types will not be created.

    2. Think about restructuring the query to make it more readable:

      var query = 
          from holding in dHoldList 
          join client in clientList on holding.ClientNo equals client.ClientNo into clj 
          join matter in matterList on holding.ClientNo equals matter.ClientNo into mlj 
          join stk in stockList on holding.Sedol equals stk.Sedol into slj 
      
          from cl in clj.DefaultIfEmpty() 
          from ml in mlj.DefaultIfEmpty() 
          from sl in slj.DefaultIfEmpty() 
          select new GeneralHoldingsReport() 
          { 
              ClientNo = holding.ClientNo, 
              Depot = holding.Depot, 
              HoldingSedol = holding.Sedol, 
              Value = holding.ValueOfStock, 
              NoOfUnits = holding.QuantityHeld, 
              ClientName = (cl == null ? null : cl.ClientName), 
              CountryOfResidence = (cl == null ? null : cl.CountryOfResidence), 
              BG = (cl == null ? null : cl.BusinessGetter), 
              ClientStockValue = (ml == null ? 0 : ml.FullValueOfPortfolio), 
              StockName = (sl == null ? null : sl.R1.Trim() + " " + sl.R2.Trim()) 
          }; 
      

      This will not have any impact on memory footprint but it does have an impact on readability and maintainability.


    If I would write code to achieve your goal, especially with that many objects involved, I wouldn’t use a join. I would use hash tables. This would be dramatically faster.

    Something like this (untested):

    var clients = clientList.ToDictionary(x => x.ClientNo);
    var matters = matterList.ToDictionary(x => x.ClientNo);
    var stocks = stockList.ToDictionary(x => x.Sedol);
    
    var reportList = new List<GeneralHoldingsReport>(dHoldList.Count);
    Client client;
    Matter matter;
    Stock stock;    
    
    foreach(var holding in dHoldList)
    {
        if(!clients.TryGetValue(holding.ClientNo, out client))
            client = null;
        if(!matters.TryGetValue(holding.ClientNo, out matter))
            matter = null;
        if(!stocks.TryGetValue(holding.Sedol, out stock))
            stock = null;
        reportList.Add(new GeneralHoldingsReport()     
                       {     
                           ClientNo = holding.ClientNo,     
                           Depot = holding.Depot,     
                           HoldingSedol = holding.Sedol,     
                           Value = holding.ValueOfStock,     
                           NoOfUnits = holding.QuantityHeld,     
                           ClientName = (client == null ? null :
                                         client.ClientName),     
                           CountryOfResidence = (client == null ? null : 
                                                 client.CountryOfResidence),     
                           BG = (client == null ? null : 
                                 client.BusinessGetter),     
                           ClientStockValue = (matter == null ? 0 : 
                                               matter.FullValueOfPortfolio),     
                           StockName = (stock == null ? null : 
                                        stock.R1.Trim() + " " 
                                        + stock.R2.Trim())     
                       });
    } 
    

    The usage of ToDictionary will result in a crash if there is more than one client or matter per client number and more than one stock per sedol.

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

Sidebar

Related Questions

I have the following MySQL query statement: SELECT * FROM table1 INNER JOIN table2
i have the following LINQ statement: var query =(from item in _itemRepository.FindAll() where item.Id
I have the following query: SELECT * FROM quotes INNER JOIN quotes_panels ON quotes.wp_user_id
I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
I have the following query SELECT * FROM attend RIGHT OUTER JOIN noattend ON
I have the following query: $img_sub_qry = SELECT * FROM images JOIN imagsub ON
I have the following group by linq statement from c in Categories join p
We have the following query to give us a left outer join: (from t0
I have the following statement SELECT b.Prig FROM Tirr tr JOIN Bud b ON
I have the following SQL statement: SELECT [id], [name] FROM [dbo.test_db_002] t1 LEFT JOIN

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.