Use linq to select a range that overlaps another range





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a list that contains a number of 'bands', as follows:



var bands = new List<Band>();

bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));
bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));
bands.Add(new Band(6, 6, 1, 200, 2000, 100, 210));
bands.Add(new Band(7, 7, 1, 200, 2000, 100, 192));
bands.Add(new Band(8, 8, 1, 200, 2000, 100, 178));
bands.Add(new Band(9, 9, 1, 200, 2000, 100, 167));
bands.Add(new Band(10, 10, 1, 200, 2000, 100, 158));
bands.Add(new Band(11, 11, 1, 200, 2000, 100, 150.5));
bands.Add(new Band(12, 12, 1, 200, 999, 100, 140));
bands.Add(new Band(12, 18, 3, 1000, 3000, 100, 71.3));
bands.Add(new Band(24, 24, 6, 1000, 3000, 100, 71.3));


The important columns are 4 and 5 - the first band values being 100 and 199.



Presently, I have a query that selects one or more bands based on a single parameter:



public static IEnumerable<Quote> ForAmount(int amount)
{
return Repository.Bands
.Where(x => amount >= x.MinAmount && amount <= x.MaxAmount)
.YieldTerms(amount); // this does something with the selected data
}


However, I'd like to pull out bands that fall within a low and high range.



New signature:



public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
{
// Query
}


So, for instance Low = 100 and High = 500.



Given the example high/low values, the following band would be selected (100 - 500 overlaps 100 - 199).



bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));


So would the following band (100 - 500 overlaps 200 - 2000).



bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));


I'm sure this is easily done, but I've got brain fog right now, so any help appreciated.










share|improve this question























  • Just to clarify: In your example, all bands except the last two overlap the 100-500 range, correct?

    – elgonzo
    Nov 16 '18 at 15:18











  • @elgonzo - yes that would be correct for the example.

    – John Ohara
    Nov 16 '18 at 15:20


















0















I have a list that contains a number of 'bands', as follows:



var bands = new List<Band>();

bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));
bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));
bands.Add(new Band(6, 6, 1, 200, 2000, 100, 210));
bands.Add(new Band(7, 7, 1, 200, 2000, 100, 192));
bands.Add(new Band(8, 8, 1, 200, 2000, 100, 178));
bands.Add(new Band(9, 9, 1, 200, 2000, 100, 167));
bands.Add(new Band(10, 10, 1, 200, 2000, 100, 158));
bands.Add(new Band(11, 11, 1, 200, 2000, 100, 150.5));
bands.Add(new Band(12, 12, 1, 200, 999, 100, 140));
bands.Add(new Band(12, 18, 3, 1000, 3000, 100, 71.3));
bands.Add(new Band(24, 24, 6, 1000, 3000, 100, 71.3));


The important columns are 4 and 5 - the first band values being 100 and 199.



Presently, I have a query that selects one or more bands based on a single parameter:



public static IEnumerable<Quote> ForAmount(int amount)
{
return Repository.Bands
.Where(x => amount >= x.MinAmount && amount <= x.MaxAmount)
.YieldTerms(amount); // this does something with the selected data
}


However, I'd like to pull out bands that fall within a low and high range.



New signature:



public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
{
// Query
}


So, for instance Low = 100 and High = 500.



Given the example high/low values, the following band would be selected (100 - 500 overlaps 100 - 199).



bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));


So would the following band (100 - 500 overlaps 200 - 2000).



bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));


I'm sure this is easily done, but I've got brain fog right now, so any help appreciated.










share|improve this question























  • Just to clarify: In your example, all bands except the last two overlap the 100-500 range, correct?

    – elgonzo
    Nov 16 '18 at 15:18











  • @elgonzo - yes that would be correct for the example.

    – John Ohara
    Nov 16 '18 at 15:20














0












0








0








I have a list that contains a number of 'bands', as follows:



var bands = new List<Band>();

bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));
bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));
bands.Add(new Band(6, 6, 1, 200, 2000, 100, 210));
bands.Add(new Band(7, 7, 1, 200, 2000, 100, 192));
bands.Add(new Band(8, 8, 1, 200, 2000, 100, 178));
bands.Add(new Band(9, 9, 1, 200, 2000, 100, 167));
bands.Add(new Band(10, 10, 1, 200, 2000, 100, 158));
bands.Add(new Band(11, 11, 1, 200, 2000, 100, 150.5));
bands.Add(new Band(12, 12, 1, 200, 999, 100, 140));
bands.Add(new Band(12, 18, 3, 1000, 3000, 100, 71.3));
bands.Add(new Band(24, 24, 6, 1000, 3000, 100, 71.3));


The important columns are 4 and 5 - the first band values being 100 and 199.



Presently, I have a query that selects one or more bands based on a single parameter:



public static IEnumerable<Quote> ForAmount(int amount)
{
return Repository.Bands
.Where(x => amount >= x.MinAmount && amount <= x.MaxAmount)
.YieldTerms(amount); // this does something with the selected data
}


However, I'd like to pull out bands that fall within a low and high range.



New signature:



public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
{
// Query
}


So, for instance Low = 100 and High = 500.



Given the example high/low values, the following band would be selected (100 - 500 overlaps 100 - 199).



bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));


So would the following band (100 - 500 overlaps 200 - 2000).



bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));


I'm sure this is easily done, but I've got brain fog right now, so any help appreciated.










share|improve this question














I have a list that contains a number of 'bands', as follows:



var bands = new List<Band>();

bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));
bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));
bands.Add(new Band(6, 6, 1, 200, 2000, 100, 210));
bands.Add(new Band(7, 7, 1, 200, 2000, 100, 192));
bands.Add(new Band(8, 8, 1, 200, 2000, 100, 178));
bands.Add(new Band(9, 9, 1, 200, 2000, 100, 167));
bands.Add(new Band(10, 10, 1, 200, 2000, 100, 158));
bands.Add(new Band(11, 11, 1, 200, 2000, 100, 150.5));
bands.Add(new Band(12, 12, 1, 200, 999, 100, 140));
bands.Add(new Band(12, 18, 3, 1000, 3000, 100, 71.3));
bands.Add(new Band(24, 24, 6, 1000, 3000, 100, 71.3));


The important columns are 4 and 5 - the first band values being 100 and 199.



Presently, I have a query that selects one or more bands based on a single parameter:



public static IEnumerable<Quote> ForAmount(int amount)
{
return Repository.Bands
.Where(x => amount >= x.MinAmount && amount <= x.MaxAmount)
.YieldTerms(amount); // this does something with the selected data
}


However, I'd like to pull out bands that fall within a low and high range.



New signature:



public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
{
// Query
}


So, for instance Low = 100 and High = 500.



Given the example high/low values, the following band would be selected (100 - 500 overlaps 100 - 199).



bands.Add(new Band(1, 12, 1, 100, 199, 100, 292));


So would the following band (100 - 500 overlaps 200 - 2000).



bands.Add(new Band(1, 5, 1, 200, 1000, 100, 292));


I'm sure this is easily done, but I've got brain fog right now, so any help appreciated.







c# linq






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 15:13









John OharaJohn Ohara

1,04011124




1,04011124













  • Just to clarify: In your example, all bands except the last two overlap the 100-500 range, correct?

    – elgonzo
    Nov 16 '18 at 15:18











  • @elgonzo - yes that would be correct for the example.

    – John Ohara
    Nov 16 '18 at 15:20



















  • Just to clarify: In your example, all bands except the last two overlap the 100-500 range, correct?

    – elgonzo
    Nov 16 '18 at 15:18











  • @elgonzo - yes that would be correct for the example.

    – John Ohara
    Nov 16 '18 at 15:20

















Just to clarify: In your example, all bands except the last two overlap the 100-500 range, correct?

– elgonzo
Nov 16 '18 at 15:18





Just to clarify: In your example, all bands except the last two overlap the 100-500 range, correct?

– elgonzo
Nov 16 '18 at 15:18













@elgonzo - yes that would be correct for the example.

– John Ohara
Nov 16 '18 at 15:20





@elgonzo - yes that would be correct for the example.

– John Ohara
Nov 16 '18 at 15:20












2 Answers
2






active

oldest

votes


















2














Repository.Bands
.Where(x => x.MinAmount <= High && x.MaxAmount >= Low)


should do it






share|improve this answer
























  • This would be if you needed both bounds to be between Low and High, which does not seem to be what OP wants

    – Rafalon
    Nov 16 '18 at 15:24











  • @Rafalon, from the question: "I'd like to pull out bands that fall within a low and high range" (emphasis mine) Saying "overlap" does not necessarily mean restriction to "partial overlap"

    – elgonzo
    Nov 16 '18 at 15:25













  • @elgonzo "100 - 500 overlaps 200 - 2000" (emphasis mine)

    – Rafalon
    Nov 16 '18 at 15:26













  • To clarify guys, where any bands intersects my parameter range - sorry for any confusion.

    – John Ohara
    Nov 16 '18 at 15:27











  • @Rafalon, yeah but where do you derive that only a partial overlap is an overlap, whereas a complete overlap is not an overlap?

    – elgonzo
    Nov 16 '18 at 15:27





















0














I have demonstrated something similar to your example in dotnet fiddle;
https://dotnetfiddle.net/dHr2Nn



This should be working.



public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
{
return Repository.Bands
.Where(x => x.MinAmount <= highAmount && x.MinAmount >= lowAmount)
|| (x.MaxAmount >= lowAmount && x.MaxAmount <= highAmount))
.YieldTerms(...); // this does something with the selected data
}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340556%2fuse-linq-to-select-a-range-that-overlaps-another-range%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Repository.Bands
    .Where(x => x.MinAmount <= High && x.MaxAmount >= Low)


    should do it






    share|improve this answer
























    • This would be if you needed both bounds to be between Low and High, which does not seem to be what OP wants

      – Rafalon
      Nov 16 '18 at 15:24











    • @Rafalon, from the question: "I'd like to pull out bands that fall within a low and high range" (emphasis mine) Saying "overlap" does not necessarily mean restriction to "partial overlap"

      – elgonzo
      Nov 16 '18 at 15:25













    • @elgonzo "100 - 500 overlaps 200 - 2000" (emphasis mine)

      – Rafalon
      Nov 16 '18 at 15:26













    • To clarify guys, where any bands intersects my parameter range - sorry for any confusion.

      – John Ohara
      Nov 16 '18 at 15:27











    • @Rafalon, yeah but where do you derive that only a partial overlap is an overlap, whereas a complete overlap is not an overlap?

      – elgonzo
      Nov 16 '18 at 15:27


















    2














    Repository.Bands
    .Where(x => x.MinAmount <= High && x.MaxAmount >= Low)


    should do it






    share|improve this answer
























    • This would be if you needed both bounds to be between Low and High, which does not seem to be what OP wants

      – Rafalon
      Nov 16 '18 at 15:24











    • @Rafalon, from the question: "I'd like to pull out bands that fall within a low and high range" (emphasis mine) Saying "overlap" does not necessarily mean restriction to "partial overlap"

      – elgonzo
      Nov 16 '18 at 15:25













    • @elgonzo "100 - 500 overlaps 200 - 2000" (emphasis mine)

      – Rafalon
      Nov 16 '18 at 15:26













    • To clarify guys, where any bands intersects my parameter range - sorry for any confusion.

      – John Ohara
      Nov 16 '18 at 15:27











    • @Rafalon, yeah but where do you derive that only a partial overlap is an overlap, whereas a complete overlap is not an overlap?

      – elgonzo
      Nov 16 '18 at 15:27
















    2












    2








    2







    Repository.Bands
    .Where(x => x.MinAmount <= High && x.MaxAmount >= Low)


    should do it






    share|improve this answer













    Repository.Bands
    .Where(x => x.MinAmount <= High && x.MaxAmount >= Low)


    should do it







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 15:17









    Klaus GütterKlaus Gütter

    2,59221422




    2,59221422













    • This would be if you needed both bounds to be between Low and High, which does not seem to be what OP wants

      – Rafalon
      Nov 16 '18 at 15:24











    • @Rafalon, from the question: "I'd like to pull out bands that fall within a low and high range" (emphasis mine) Saying "overlap" does not necessarily mean restriction to "partial overlap"

      – elgonzo
      Nov 16 '18 at 15:25













    • @elgonzo "100 - 500 overlaps 200 - 2000" (emphasis mine)

      – Rafalon
      Nov 16 '18 at 15:26













    • To clarify guys, where any bands intersects my parameter range - sorry for any confusion.

      – John Ohara
      Nov 16 '18 at 15:27











    • @Rafalon, yeah but where do you derive that only a partial overlap is an overlap, whereas a complete overlap is not an overlap?

      – elgonzo
      Nov 16 '18 at 15:27





















    • This would be if you needed both bounds to be between Low and High, which does not seem to be what OP wants

      – Rafalon
      Nov 16 '18 at 15:24











    • @Rafalon, from the question: "I'd like to pull out bands that fall within a low and high range" (emphasis mine) Saying "overlap" does not necessarily mean restriction to "partial overlap"

      – elgonzo
      Nov 16 '18 at 15:25













    • @elgonzo "100 - 500 overlaps 200 - 2000" (emphasis mine)

      – Rafalon
      Nov 16 '18 at 15:26













    • To clarify guys, where any bands intersects my parameter range - sorry for any confusion.

      – John Ohara
      Nov 16 '18 at 15:27











    • @Rafalon, yeah but where do you derive that only a partial overlap is an overlap, whereas a complete overlap is not an overlap?

      – elgonzo
      Nov 16 '18 at 15:27



















    This would be if you needed both bounds to be between Low and High, which does not seem to be what OP wants

    – Rafalon
    Nov 16 '18 at 15:24





    This would be if you needed both bounds to be between Low and High, which does not seem to be what OP wants

    – Rafalon
    Nov 16 '18 at 15:24













    @Rafalon, from the question: "I'd like to pull out bands that fall within a low and high range" (emphasis mine) Saying "overlap" does not necessarily mean restriction to "partial overlap"

    – elgonzo
    Nov 16 '18 at 15:25







    @Rafalon, from the question: "I'd like to pull out bands that fall within a low and high range" (emphasis mine) Saying "overlap" does not necessarily mean restriction to "partial overlap"

    – elgonzo
    Nov 16 '18 at 15:25















    @elgonzo "100 - 500 overlaps 200 - 2000" (emphasis mine)

    – Rafalon
    Nov 16 '18 at 15:26







    @elgonzo "100 - 500 overlaps 200 - 2000" (emphasis mine)

    – Rafalon
    Nov 16 '18 at 15:26















    To clarify guys, where any bands intersects my parameter range - sorry for any confusion.

    – John Ohara
    Nov 16 '18 at 15:27





    To clarify guys, where any bands intersects my parameter range - sorry for any confusion.

    – John Ohara
    Nov 16 '18 at 15:27













    @Rafalon, yeah but where do you derive that only a partial overlap is an overlap, whereas a complete overlap is not an overlap?

    – elgonzo
    Nov 16 '18 at 15:27







    @Rafalon, yeah but where do you derive that only a partial overlap is an overlap, whereas a complete overlap is not an overlap?

    – elgonzo
    Nov 16 '18 at 15:27















    0














    I have demonstrated something similar to your example in dotnet fiddle;
    https://dotnetfiddle.net/dHr2Nn



    This should be working.



    public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
    {
    return Repository.Bands
    .Where(x => x.MinAmount <= highAmount && x.MinAmount >= lowAmount)
    || (x.MaxAmount >= lowAmount && x.MaxAmount <= highAmount))
    .YieldTerms(...); // this does something with the selected data
    }





    share|improve this answer




























      0














      I have demonstrated something similar to your example in dotnet fiddle;
      https://dotnetfiddle.net/dHr2Nn



      This should be working.



      public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
      {
      return Repository.Bands
      .Where(x => x.MinAmount <= highAmount && x.MinAmount >= lowAmount)
      || (x.MaxAmount >= lowAmount && x.MaxAmount <= highAmount))
      .YieldTerms(...); // this does something with the selected data
      }





      share|improve this answer


























        0












        0








        0







        I have demonstrated something similar to your example in dotnet fiddle;
        https://dotnetfiddle.net/dHr2Nn



        This should be working.



        public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
        {
        return Repository.Bands
        .Where(x => x.MinAmount <= highAmount && x.MinAmount >= lowAmount)
        || (x.MaxAmount >= lowAmount && x.MaxAmount <= highAmount))
        .YieldTerms(...); // this does something with the selected data
        }





        share|improve this answer













        I have demonstrated something similar to your example in dotnet fiddle;
        https://dotnetfiddle.net/dHr2Nn



        This should be working.



        public static IEnumerable<Quote> ForAmount(int lowAmount, int highAmount)
        {
        return Repository.Bands
        .Where(x => x.MinAmount <= highAmount && x.MinAmount >= lowAmount)
        || (x.MaxAmount >= lowAmount && x.MaxAmount <= highAmount))
        .YieldTerms(...); // this does something with the selected data
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 15:49









        AyberkAyberk

        484211




        484211






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340556%2fuse-linq-to-select-a-range-that-overlaps-another-range%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma