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;
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
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.
|
show 3 more comments
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
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 withSystem.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
|
show 3 more comments
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
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
c# .net asp.net-web-api .net-core
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 withSystem.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
|
show 3 more comments
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 withSystem.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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 27 at 14:38
Jesus SalasJesus Salas
4622 silver badges11 bronze badges
4622 silver badges11 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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