How do you cater for AND and OR searches in a WebApi query string?How do you sort a dictionary by value?How do you give a C# Auto-Property a default value?How to escape braces (curly brackets) in a format string in .NETHow do you convert a byte array to a hexadecimal string, and vice versa?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How would you count occurrences of a string (actually a char) within a string?How to convert UTF-8 byte[] to string?How can I generate random alphanumeric strings?How do I turn a C# object into a JSON string in .NET?Why not inherit from List<T>?

Why does the salt in the oceans not sink to the bottom?

Sci-fi short story: plants attracting spaceship and using them as a agents of pollination between two planets

A team with high solidarity

How to correct errors in proofs of an accepted paper

Why are MEMS in QFN packages?

In a script how can I signal who's winning the argument?

Is a sentence true for two substructures also true for their intersection?

What is the relationship between the theme songs in Sherlock Holmes (2009 movie) and Sherlock (BBC series)?

What is the best word describing the nature of expiring in a short amount of time, connoting "losing public attention"?

Grid/table with lots of buttons

Travelling from Venice to Budapest, making a stop in Croatia

How to Sow[] until I've Reap[]'d enough?

Download file from URL to Safehouse

How to repair basic cable/wire issue for household appliances

Can't understand how static works exactly

High income and difficulty during interviews

How could Barty Crouch Jr. have run out of Polyjuice Potion at the end of the Goblet of Fire movie?

Inverse Colombian Function

Is the apartment I want to rent a scam?

I have a domain, static IP address and many devices I'd like to access outside my house. How do I route them?

What's the explanation for this joke about a three-legged dog that walks into a bar?

Are rockets faster than airplanes?

Considerations when providing money to one child now, and the other later?

Found more old paper shares from broken up companies



How do you cater for AND and OR searches in a WebApi query string?


How do you sort a dictionary by value?How do you give a C# Auto-Property a default value?How to escape braces (curly brackets) in a format string in .NETHow do you convert a byte array to a hexadecimal string, and vice versa?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How would you count occurrences of a string (actually a char) within a string?How to convert UTF-8 byte[] to string?How can I generate random alphanumeric strings?How do I turn a C# object into a JSON string in .NET?Why not inherit from List<T>?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















How do you cater for AND and OR searches in a WebApi query string using Get in C#?



For example, for searches such as firstName=John OR firstName=Simon AND Country=UK OR Country=Canada OR LastName=Jones Or Age>=65










share|improve this question















migrated from softwareengineering.stackexchange.com Mar 26 at 15:19


This question came from our site for professionals, academics, and students working within the systems development life cycle.













  • 2





    OData?

    – Damien_The_Unbeliever
    Mar 26 at 15:23











  • @Damien_The_Unbeliever: Can you be a bit more specific? There's a lot of information at the site you linked; you're basically quoting all of OData. Brief guidance on the specifics in OData that pertain (bonus points for a code sample) would make a good answer to the OP's question.

    – Robert Harvey
    Mar 26 at 15:30












  • Are you using EF? I managed to do this with System.Linq.Dynamic.Core (github.com/StefH/System.Linq.Dynamic.Core)

    – chakeda
    Mar 26 at 15:47











  • @Damien_The_Unbeliever is right, WebAPI supports OData that will be the best way to do it this link provide the information on how to setup a V3 WebApi endpoint docs.microsoft.com/en-us/aspnet/web-api/overview/…

    – Jesus Salas
    Mar 26 at 16:01











  • Correction on my comment, the doc provides the info to setup OData V4 EndPoint (V3 as well)

    – Jesus Salas
    Mar 26 at 16:08

















3















How do you cater for AND and OR searches in a WebApi query string using Get in C#?



For example, for searches such as firstName=John OR firstName=Simon AND Country=UK OR Country=Canada OR LastName=Jones Or Age>=65










share|improve this question















migrated from softwareengineering.stackexchange.com Mar 26 at 15:19


This question came from our site for professionals, academics, and students working within the systems development life cycle.













  • 2





    OData?

    – Damien_The_Unbeliever
    Mar 26 at 15:23











  • @Damien_The_Unbeliever: Can you be a bit more specific? There's a lot of information at the site you linked; you're basically quoting all of OData. Brief guidance on the specifics in OData that pertain (bonus points for a code sample) would make a good answer to the OP's question.

    – Robert Harvey
    Mar 26 at 15:30












  • Are you using EF? I managed to do this with System.Linq.Dynamic.Core (github.com/StefH/System.Linq.Dynamic.Core)

    – chakeda
    Mar 26 at 15:47











  • @Damien_The_Unbeliever is right, WebAPI supports OData that will be the best way to do it this link provide the information on how to setup a V3 WebApi endpoint docs.microsoft.com/en-us/aspnet/web-api/overview/…

    – Jesus Salas
    Mar 26 at 16:01











  • Correction on my comment, the doc provides the info to setup OData V4 EndPoint (V3 as well)

    – Jesus Salas
    Mar 26 at 16:08













3












3








3


0






How do you cater for AND and OR searches in a WebApi query string using Get in C#?



For example, for searches such as firstName=John OR firstName=Simon AND Country=UK OR Country=Canada OR LastName=Jones Or Age>=65










share|improve this question
















How do you cater for AND and OR searches in a WebApi query string using Get in C#?



For example, for searches such as firstName=John OR firstName=Simon AND Country=UK OR Country=Canada OR LastName=Jones Or Age>=65







c# .net asp.net-web-api .net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 15:20









Robert Harvey

152k37 gold badges290 silver badges426 bronze badges




152k37 gold badges290 silver badges426 bronze badges










asked Mar 26 at 15:03









D.ManD.Man

217 bronze badges




217 bronze badges




migrated from softwareengineering.stackexchange.com Mar 26 at 15:19


This question came from our site for professionals, academics, and students working within the systems development life cycle.









migrated from softwareengineering.stackexchange.com Mar 26 at 15:19


This question came from our site for professionals, academics, and students working within the systems development life cycle.









  • 2





    OData?

    – Damien_The_Unbeliever
    Mar 26 at 15:23











  • @Damien_The_Unbeliever: Can you be a bit more specific? There's a lot of information at the site you linked; you're basically quoting all of OData. Brief guidance on the specifics in OData that pertain (bonus points for a code sample) would make a good answer to the OP's question.

    – Robert Harvey
    Mar 26 at 15:30












  • Are you using EF? I managed to do this with System.Linq.Dynamic.Core (github.com/StefH/System.Linq.Dynamic.Core)

    – chakeda
    Mar 26 at 15:47











  • @Damien_The_Unbeliever is right, WebAPI supports OData that will be the best way to do it this link provide the information on how to setup a V3 WebApi endpoint docs.microsoft.com/en-us/aspnet/web-api/overview/…

    – Jesus Salas
    Mar 26 at 16:01











  • Correction on my comment, the doc provides the info to setup OData V4 EndPoint (V3 as well)

    – Jesus Salas
    Mar 26 at 16:08












  • 2





    OData?

    – Damien_The_Unbeliever
    Mar 26 at 15:23











  • @Damien_The_Unbeliever: Can you be a bit more specific? There's a lot of information at the site you linked; you're basically quoting all of OData. Brief guidance on the specifics in OData that pertain (bonus points for a code sample) would make a good answer to the OP's question.

    – Robert Harvey
    Mar 26 at 15:30












  • Are you using EF? I managed to do this with System.Linq.Dynamic.Core (github.com/StefH/System.Linq.Dynamic.Core)

    – chakeda
    Mar 26 at 15:47











  • @Damien_The_Unbeliever is right, WebAPI supports OData that will be the best way to do it this link provide the information on how to setup a V3 WebApi endpoint docs.microsoft.com/en-us/aspnet/web-api/overview/…

    – Jesus Salas
    Mar 26 at 16:01











  • Correction on my comment, the doc provides the info to setup OData V4 EndPoint (V3 as well)

    – Jesus Salas
    Mar 26 at 16:08







2




2





OData?

– Damien_The_Unbeliever
Mar 26 at 15:23





OData?

– Damien_The_Unbeliever
Mar 26 at 15:23













@Damien_The_Unbeliever: Can you be a bit more specific? There's a lot of information at the site you linked; you're basically quoting all of OData. Brief guidance on the specifics in OData that pertain (bonus points for a code sample) would make a good answer to the OP's question.

– Robert Harvey
Mar 26 at 15:30






@Damien_The_Unbeliever: Can you be a bit more specific? There's a lot of information at the site you linked; you're basically quoting all of OData. Brief guidance on the specifics in OData that pertain (bonus points for a code sample) would make a good answer to the OP's question.

– Robert Harvey
Mar 26 at 15:30














Are you using EF? I managed to do this with System.Linq.Dynamic.Core (github.com/StefH/System.Linq.Dynamic.Core)

– chakeda
Mar 26 at 15:47





Are you using EF? I managed to do this with System.Linq.Dynamic.Core (github.com/StefH/System.Linq.Dynamic.Core)

– chakeda
Mar 26 at 15:47













@Damien_The_Unbeliever is right, WebAPI supports OData that will be the best way to do it this link provide the information on how to setup a V3 WebApi endpoint docs.microsoft.com/en-us/aspnet/web-api/overview/…

– Jesus Salas
Mar 26 at 16:01





@Damien_The_Unbeliever is right, WebAPI supports OData that will be the best way to do it this link provide the information on how to setup a V3 WebApi endpoint docs.microsoft.com/en-us/aspnet/web-api/overview/…

– Jesus Salas
Mar 26 at 16:01













Correction on my comment, the doc provides the info to setup OData V4 EndPoint (V3 as well)

– Jesus Salas
Mar 26 at 16:08





Correction on my comment, the doc provides the info to setup OData V4 EndPoint (V3 as well)

– Jesus Salas
Mar 26 at 16:08












1 Answer
1






active

oldest

votes


















0














If you want to support this kind of queries 'properly' the best path of action is to enable OData on your WebAPI2 Controller.



The OData in ASP.NET Web API documentation provides all the information you need to configure OData V4 (V3 as well) in your WebAPI project, and provides examples on how to implement your Controllers Actions (Supporting OData Query Options in ASP.NET Web API 2s)



From the above link we can see next code snippet:



public class ProductsController : ApiController

[Queryable]
IQueryable<Product> Get()



How the Get() implementation looks when you are using Entity Framework?



 public class ProductsController : ApiController

[Queryable]
IQueryable<Product> Get()
var ef = new myDbContext();
return ef.Products; // <-- DbSet<Product>




And how a query string will look to query this WebAPI EndPoint?



https://[domain]/api/v1/Products?$filter=firstName eq 'john' or firstName eq 'Simon' and country eq 'UK'


Is as simple as the above because the .Net OData stack will parse out of the box the $filter (and any other querystring element that is supported by the OData specification) and apply the query (and the others) to the IQueryable object by itself



If you are not using EF, or any other system that provides access to a IQueryable you can intercept this out-of-the-box behavior and get full control of how this parsing happen to adapt the execution to your needs.






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%2f55360656%2fhow-do-you-cater-for-and-and-or-searches-in-a-webapi-query-string%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you want to support this kind of queries 'properly' the best path of action is to enable OData on your WebAPI2 Controller.



    The OData in ASP.NET Web API documentation provides all the information you need to configure OData V4 (V3 as well) in your WebAPI project, and provides examples on how to implement your Controllers Actions (Supporting OData Query Options in ASP.NET Web API 2s)



    From the above link we can see next code snippet:



    public class ProductsController : ApiController

    [Queryable]
    IQueryable<Product> Get()



    How the Get() implementation looks when you are using Entity Framework?



     public class ProductsController : ApiController

    [Queryable]
    IQueryable<Product> Get()
    var ef = new myDbContext();
    return ef.Products; // <-- DbSet<Product>




    And how a query string will look to query this WebAPI EndPoint?



    https://[domain]/api/v1/Products?$filter=firstName eq 'john' or firstName eq 'Simon' and country eq 'UK'


    Is as simple as the above because the .Net OData stack will parse out of the box the $filter (and any other querystring element that is supported by the OData specification) and apply the query (and the others) to the IQueryable object by itself



    If you are not using EF, or any other system that provides access to a IQueryable you can intercept this out-of-the-box behavior and get full control of how this parsing happen to adapt the execution to your needs.






    share|improve this answer



























      0














      If you want to support this kind of queries 'properly' the best path of action is to enable OData on your WebAPI2 Controller.



      The OData in ASP.NET Web API documentation provides all the information you need to configure OData V4 (V3 as well) in your WebAPI project, and provides examples on how to implement your Controllers Actions (Supporting OData Query Options in ASP.NET Web API 2s)



      From the above link we can see next code snippet:



      public class ProductsController : ApiController

      [Queryable]
      IQueryable<Product> Get()



      How the Get() implementation looks when you are using Entity Framework?



       public class ProductsController : ApiController

      [Queryable]
      IQueryable<Product> Get()
      var ef = new myDbContext();
      return ef.Products; // <-- DbSet<Product>




      And how a query string will look to query this WebAPI EndPoint?



      https://[domain]/api/v1/Products?$filter=firstName eq 'john' or firstName eq 'Simon' and country eq 'UK'


      Is as simple as the above because the .Net OData stack will parse out of the box the $filter (and any other querystring element that is supported by the OData specification) and apply the query (and the others) to the IQueryable object by itself



      If you are not using EF, or any other system that provides access to a IQueryable you can intercept this out-of-the-box behavior and get full control of how this parsing happen to adapt the execution to your needs.






      share|improve this answer

























        0












        0








        0







        If you want to support this kind of queries 'properly' the best path of action is to enable OData on your WebAPI2 Controller.



        The OData in ASP.NET Web API documentation provides all the information you need to configure OData V4 (V3 as well) in your WebAPI project, and provides examples on how to implement your Controllers Actions (Supporting OData Query Options in ASP.NET Web API 2s)



        From the above link we can see next code snippet:



        public class ProductsController : ApiController

        [Queryable]
        IQueryable<Product> Get()



        How the Get() implementation looks when you are using Entity Framework?



         public class ProductsController : ApiController

        [Queryable]
        IQueryable<Product> Get()
        var ef = new myDbContext();
        return ef.Products; // <-- DbSet<Product>




        And how a query string will look to query this WebAPI EndPoint?



        https://[domain]/api/v1/Products?$filter=firstName eq 'john' or firstName eq 'Simon' and country eq 'UK'


        Is as simple as the above because the .Net OData stack will parse out of the box the $filter (and any other querystring element that is supported by the OData specification) and apply the query (and the others) to the IQueryable object by itself



        If you are not using EF, or any other system that provides access to a IQueryable you can intercept this out-of-the-box behavior and get full control of how this parsing happen to adapt the execution to your needs.






        share|improve this answer













        If you want to support this kind of queries 'properly' the best path of action is to enable OData on your WebAPI2 Controller.



        The OData in ASP.NET Web API documentation provides all the information you need to configure OData V4 (V3 as well) in your WebAPI project, and provides examples on how to implement your Controllers Actions (Supporting OData Query Options in ASP.NET Web API 2s)



        From the above link we can see next code snippet:



        public class ProductsController : ApiController

        [Queryable]
        IQueryable<Product> Get()



        How the Get() implementation looks when you are using Entity Framework?



         public class ProductsController : ApiController

        [Queryable]
        IQueryable<Product> Get()
        var ef = new myDbContext();
        return ef.Products; // <-- DbSet<Product>




        And how a query string will look to query this WebAPI EndPoint?



        https://[domain]/api/v1/Products?$filter=firstName eq 'john' or firstName eq 'Simon' and country eq 'UK'


        Is as simple as the above because the .Net OData stack will parse out of the box the $filter (and any other querystring element that is supported by the OData specification) and apply the query (and the others) to the IQueryable object by itself



        If you are not using EF, or any other system that provides access to a IQueryable you can intercept this out-of-the-box behavior and get full control of how this parsing happen to adapt the execution to your needs.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 14:38









        Jesus SalasJesus Salas

        4622 silver badges11 bronze badges




        4622 silver badges11 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            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%2f55360656%2fhow-do-you-cater-for-and-and-or-searches-in-a-webapi-query-string%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해