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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:40:55+00:00 2026-05-15T22:40:55+00:00

This is related to Another Question , which I think really gets at a

  • 0

This is related to Another Question, which I think really gets at a much simpler problem so I’m asking the simpler question here in the hopes it will help me solve the more complex one.

I would like to be able to create a grouping in a linq to sql query that groups based on a range of data within another set of data. However, i think it should work just as well in linq to objects, so let’s just go with that.

Imagine you have two lists containing values

{100, 110, 120, 130, 140, 150, 160, 170}
{115, 145, 180}

Now, I would like to group the first list by the second as ranges (values that are between each group). That is, I would like a grouping like this (the 0 is implied):

{0}   {100, 110}
{115} {120, 130, 140}
{145} {150, 160, 170}
{180}

I’m almost certain i’m misusing terminology, and probably have a misunderstanding of how linq group by operator works, but if you get what I mean, I’d love some suggestions. Thanks.

  • 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-15T22:40:56+00:00Added an answer on May 15, 2026 at 10:40 pm

    Well, you can certainly express it in LINQ easily:

    var x = from value in values
            group value by ranges.Where(x => value >= x)
                                 .DefaultIfEmpty()
                                 .Last();
    

    But I very much doubt that that will work in LINQ to SQL. Basically you’ve got to find a simple way of mapping a value to one of those categories.

    Complete example:

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    class Test
    {
        static void Main()
        {
            int[] values = {100, 110, 120, 130, 140, 150, 160, 170};
            int[] ranges = {115, 145, 180};
    
            var query = from value in values
                        group value by ranges.Where(x => value >= x)
                                             .DefaultIfEmpty()
                                             .Last();
    
            foreach (var group in query)
            {
                Console.WriteLine("{0}: {{{1}}}", group.Key, 
                                  string.Join(", ", group));
            }
        }
    }
    

    Output:

    0: {100, 110}
    115: {120, 130, 140}
    145: {150, 160, 170}
    

    Note that this won’t include any categories which don’t have any values in.

    Also note that this would be simpler (using First() instead of Last()) if you’d be happy to categorize slightly differently:

    115: {100, 110}
    145: {120, 130, 140}
    180: {150, 160, 170}
    

    In other words, if the category was defined by the first range value higher than the row value.

    EDIT: Here’s a version which gives the empty groups. It’s pretty horrible though, IMO:

    var query = from range in ranges
                join value in values
                on range equals ranges.Where(x => value >= x)
                                      .DefaultIfEmpty()
                                      .Last() into groups
                select new { Key = range, Values = groups};
    
    foreach (var group in query)
    {
        Console.WriteLine("{0}: {{{1}}}", group.Key, 
                          string.Join(", ", group.Values));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to another question I asked. In my other question I
This is related to another question I recently asked about storing a non-bare repository
I apologize in advanced if this question is too broad. In fact it's 4
I just want to ask a simple question related the GLSurfaceView and drawn objects
I originally asked this question on programmers.stackexchange.com, because I figured the answers would be
I am developing software in C#/.NET but I guess the question can be asked
Something that has been bugging me since I read an answer on another stackoverflow
I'm having a devil of a time understanding references. Consider the following code: class
I have approached you today to try and gather your opinions and experience with
It started out when I read Guido van Rossum's essay An Optimization Anecdote .

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.