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

  • Home
  • SEARCH
  • 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 3851618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:11:26+00:00 2026-05-19T17:11:26+00:00

How do you use Linq to select the record that is closest to a

  • 0

How do you use Linq to select the record that is closest to a specified date? This is for a transaction table that has a date, product id, location id, and balance.

Given these requirements:

  • Select the last transaction of the day if there were several on the specified day
  • Select the closest prior transaction if there were none on the specified day
  • Show the balance in multiple locations (eg warehouses) – each warehouse will have separate transactions
  • Show the balance for multiple products – each product will have separate transactions

Table data:

// code
Id, TransDateTime,    ProductId, WarehouseId, Balance
1,  1-Jan-2011 00:00, 1,         1,           100
2,  1-Jan-2011 00:00, 1,         2,           10
3,  2-Jan-2011 00:00, 1,         1,           150
4,  3-Jan-2011 00:00, 1,         2,           25
5,  3-Jan-2011 00:00, 2,         1,           333
6,  7-Jan-2011 00:00, 1,         1,           149
7,  7-Jan-2011 01:00, 1,         2,           30
8,  7-Jan-2011 02:00, 1,         2,           35

Test dates and outputs

Date: 1-Jan would output:
1,  1-Jan-2011 00:00, 1,         1,           100
2,  1-Jan-2011 00:00, 1,         2,           10

Date: 3-Jan would output:
3,  2-Jan-2011 00:00, 1,         1,           150 
4,  3-Jan-2011 00:00, 1,         2,           25
5,  3-Jan-2011 00:00, 2,         1,           333
// product 1, warehouse 1 wasn't sold on the 3rd
// so the row from 2-Jan is returned

Date: 7-Jan would output:
5,  3-Jan-2011 00:00, 2,         1,           333
6,  7-Jan-2011 00:00, 1,         1,           149
9,  7-Jan-2011 02:00, 1,         2,           35
// product 2, warehouse 1 wasn't sold on the 7th
// so the row from 3-Jan is returned
// product 1, warehouse 2 was sold twice on the 7th
// so the later one is used

I think it’s going to require grouping of groups (product -> warehouse -> date) or similar. Its beyond my linq ability!

  • 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-19T17:11:27+00:00Added an answer on May 19, 2026 at 5:11 pm

    Steps:
    1) Filter out transactions that happened after inputDate
    2) Group rest of transactions by product and warehouse
    3) In each group find most recent transaction
    4) Format result object

    Straightforward implementation:

    DateTime inputDate = ...;
    var result = transactions
              .Where(t => t.TransDateTime.Date <= inputDate.Date)
              .GroupBy(t => new {t.ProductId, t.WarehouseId})
              .Select(x => new {
                   x.Key,
                   LastTransaction = x.OrderByDescending(t => t.TransDateTime).First(),
              })
              .Select(x => new {
                   Id = x.LastTransaction.Id,
                   Date = x.LastTransaction.TransDateTime,
                   ProductId = x.Key.ProductId,
                   WarehouseId = x.Key.WarehouseId,
                   Balance = x.LastTransaction.Balance,
              });
    

    If you want some optimizations you can consider implementing MaxBy extension method for IEnumerable to replace x.OrderByDescending(t => t.TransDateTime).First(). It will improve performance if you have many transactions since it is O(n) instead of O(n log n). MaxBy implementation can be taken here for example: Simple LINQ question in C#

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

Sidebar

Related Questions

I want to make custom select from the database table using Linq. We use
Linq in general, has extensions methods(at IEnumerable) like Where, Select, OrderBy. But use another
How to use Linq to select and group complex child object from a parents
I'm trying to use LINQ to SQL to select a few specific columns from
While trying to use LINQ to SQL I encountered several problems. I have table
I want to use LINQ's IQueryable that gives me the query that gets only
I need to use LINQ to build a kind of weird query that uses
I've got an SQL table that stores a person record where one column is
I have a database table that represents Events. The table has 2 main fields
I am trying to update values that I use IQueryable to search my linq

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.