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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:26:45+00:00 2026-05-24T05:26:45+00:00

That is the most efficient way to perform a left outer join in LINQ

  • 0

That is the most efficient way to perform a left outer join in LINQ if I must do the following…

  • Filter Table 2 by a beginning and ending date.
  • All rows in Table 1 must remain, even if the filtering of Table 2 returns no rows.
  • The result must be grouped so that the columns from Table 2 get summed.

For example (example code variable names changed for propietary reasons), supposed I have a database with two tables. Table 1 has a list of doors with a building code, door ID and current status (open or closed) – the building code and door ID are the primary key. Table 2 has a list of events for all doors (an event is an opening or closing) plus a timestamp. So the columns are building code, door ID, timestamp, opening, closing. Opening and closing are integers with a 1 in the column for the appropriate event. There is a foreign key relationship between the two tables on the building code and door ID.

For my query I need to return a list of all the unique doors with the current door status and a sum of all the opening and closing events for a selected time period. An entry must be returned for each door, even if no events occured during the selected time period.

Below is the best LINQ code I could come up with. It works, but it seems really inefficient and hard to understand. How would you make it more efficient and easier to understand?

var query = 
    from doors in Context.Doors
    join fevents in
        (
        from events in db.Events
        where events.TimeStamp >= date1 && events.TimeStamp <= date2
        select new { events.BuildingCode, events.DoorID, events.TimeStamp, events.Opening, events.Closing }
        )
    on new { doors.BuildingCode, doors.DoorID } equals { fevents.BuildingCode, fevents.DoorID }
    into g1
    from c in g1.DefaultIfEmpty()
    group c by new
    {
        doors.BuildingCode,
        doors.DoorID,
        doors.DoorStatus
    } into g2
    select new
    {
        BuildingCode = g2.Key.BuildingCode,
        DoorID = g2.Key.DoorID,
        Status = g2.Key.DoorStatus
        NumOpenings = g2.Sum(i => (i == null ? 0 : i.Opening)),
        NumClosings = g2.Sum(i => (i == null ? 0 : i.Closing))
    };
  • 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-24T05:26:46+00:00Added an answer on May 24, 2026 at 5:26 am

    I think this is slightly easier to read

    var query = 
        from doors in Context.Doors
        from c in db.Events
                    .Where(events => doors.BuildingCode == events.BuildingCode)
                    .Where(events => doors.DoorID == events.DoorID)
                    .Where(events => events.TimeStamp >= date1 && events.TimeStamp <= date2)
                    .Select(events => new { events.BuildingCode, events.DoorID, events.TimeStamp, events.Opening, events.Closing })
                    .DefaultIfEmpty()
        group c by new
        {
            doors.BuildingCode,
            doors.DoorID,
            doors.DoorStatus
        } into g2
        select new
        {
            BuildingCode = g2.Key.BuildingCode,
            DoorID = g2.Key.DoorID,
            Status = g2.Key.DoorStatus
            NumOpenings = g2.Sum(i => (i == null ? 0 : i.Opening)),
            NumClosings = g2.Sum(i => (i == null ? 0 : i.Closing))
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the most efficient way to perform this query on a CoreData table? To
What are the most efficient SQL queries that would take an existing MySQL table
What would be the most efficient way to clean a user input that is
I'm seeking the most efficient way to make strings like that abcdefghijklmnop(longer then 15)
I need to find the most efficient way to insert a value into table
I'm trying to determine the most efficient way of performing the following query with
I have a simple question about the most efficient way to perform a particular
I know that most links should be left up to the end-user to decide
The most efficient way to code powers of two is by bit shifting of
What is the most efficient way to get a list of folders from a

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.