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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:14:25+00:00 2026-05-11T10:14:25+00:00

I need a linq expression that will find the nearest numbers (both greater or

  • 0

I need a linq expression that will find the nearest numbers (both ‘greater or equal’ and ‘less or equal’) in an array for a given number.

E.g. Array – 1, 33, 66, 100

If I have the number 10, I want to return 1 and 33. If I have the number 70, I want to return 66 and 100. If I have the number 33, I want to return 33 and 66.

I could do this with some kind of basic for loop, but this is an operation on numbers in a database, so I’d prefer a linq to sql expression.

Edit: I was actually searching for a single linq expression to achieve this, but perhaps I was being a little hopeful 🙂

  • 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. 2026-05-11T10:14:25+00:00Added an answer on May 11, 2026 at 10:14 am

    Similar to Andrews answer, but I prefer doing the filter before the OrderBy, which reduces the amount of data the query has to run through. Also OrderBy.First is the same as Min, and OrderByDescending.First is the same as Max.

    var high = list     .Where(i => i > n)     .DefaultIfEmpty()     .Min();  var low = list     .Where(i => i <= n)     .DefaultIfEmpty()     .Max(); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use Where method in Linq 2 entities that will be equal
I need to create a custom Contains LINQ expression that derives from System.Linq.Expressions.Expression that
I need to perform a LINQ query for both a time period from a
I've got two tables that need to be joined via LINQ, but they live
I am using LINQ-to-SQL for an application that queries a legacy database. I need
Need to an expression that returns only things with an I followed by either
I want to use Linq expression for some dynamic features. I need And, Or
I have a LINQ expression that's slowing down my application. I'm drawing a control,
We need to generate LINQ queries which are 100% unknown during coding (design time).
I've got a situation where I need to use LINQ's ExecuteCommand method to run

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.