{"id":20940830,"url":"https://github.com/timhanewich/timhanewich.odata","last_synced_at":"2025-09-04T05:13:56.597Z","repository":{"id":117078207,"uuid":"523848873","full_name":"TimHanewich/TimHanewich.OData","owner":"TimHanewich","description":"Lightweight .NET library for parsing, composing, and translating OData operations.","archived":false,"fork":false,"pushed_at":"2022-09-01T16:08:45.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-19T20:58:06.155Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TimHanewich.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-11T19:45:56.000Z","updated_at":"2023-03-04T04:27:26.000Z","dependencies_parsed_at":"2023-06-03T03:30:18.406Z","dependency_job_id":null,"html_url":"https://github.com/TimHanewich/TimHanewich.OData","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimHanewich%2FTimHanewich.OData","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimHanewich%2FTimHanewich.OData/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimHanewich%2FTimHanewich.OData/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimHanewich%2FTimHanewich.OData/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TimHanewich","download_url":"https://codeload.github.com/TimHanewich/TimHanewich.OData/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243335385,"owners_count":20274898,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T23:12:03.022Z","updated_at":"2025-03-13T03:40:18.637Z","avatar_url":"https://github.com/TimHanewich.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TimHanewich.OData\n**TimHanewich.OData** is a lightweight .NET library for parsing, composing, and translating OData operations. [OData, short for \"Open Data Protocol\"](https://www.odata.org/) allows the creation and consumption of queryable and interoperable REST APIs in a simple and standard way. Microsoft originally initiated OData in 2007 and OData has become the industry-embraced standard since.\n\nThis library is designed to assist with the following:\n1. Constructing an OData query and converting this to an `HttpRequestMessage` to be sent to an OData API\n2. Deconstructing an OData `HttpRequestMessage` into it's various components\n3. Translating an OData query to it's SQL equivalent that can be executed against a SQL database \n\nThis library supports the following OData parameters: `$filter`, `$select`, `$orderby`, `$count`, `$top`, `$skip`.\n\n## Read Operation\nExample read operation with $top, $filter, $select, $order:\n```\nODataOperation o = new ODataOperation();\no.Operation = DataOperation.Read;\n\n//Set table name and limit # of records\no.Resource = \"Contacts\"; //the table/resource name you are trying to read from\no.top = 25;\n\n//Select certain fields\no.select.Add(\"FirstName\");\no.select.Add(\"LastName\");\n\n//Add filter - LastName\nODataFilter filter1 = new ODataFilter();\nfilter1.ColumnName = \"LastName\";\nfilter1.Operator = ComparisonOperator.Equals;\nfilter1.SetValue(\"Smith\");\no.filter.Add(filter1);\n\n//Add filter - Age\nODataFilter filter2 = new ODataFilter();\nfilter1.LogicalOperatorPrefix = LogicalOperator.And; //Add \"and\" to this, to imply this is in addition to filter1\nfilter2.ColumnName = \"Age\";\nfilter2.Operator = ComparisonOperator.GreaterThanOrEqualTo;\nfilter2.SetValue(47);\no.filter.Add(filter2);\n\n//Order (sort) results\nODataOrder order = new ODataOrder();\norder.ColumnName = \"DateOfBirth\";\norder.Direction = OrderDirection.Descending;\no.orderby.Add(order);\n\nConsole.WriteLine(o.ToHttpRequestMessage(\"https://my_site.com/my_odata_endpoint/\").RequestUri.ToString());\n\n//https://my_site.com/my_odata_endpoint/Contacts?$filter=Age+ge+47+and+LastName+eq+%27Smith%27\u0026$select=FirstName%2cLastName\u0026$orderby=DateOfBirth+desc\u0026$top=25\n```\n\n## Create Operation\nExample of creating a `Contact` record via an OData request and converting the `ODataOperation` into an `HttpRequestMessage` that can be delivered to the OData endpoint:\n```\nODataOperation op = new ODataOperation();\nop.Operation = DataOperation.Create;\nop.Resource = \"Contact\";\n\n//Create the object with JSON that will be created\nJObject jo = new JObject();\njo.Add(\"FirstName\", \"John\");\njo.Add(\"LastName\", \"Appleseed\");\njo.Add(\"DateOfBirth\", new DateTime(1980, 1, 1));\nop.Payload = jo;\n\n//Create the HttpRequestMessage to send that contains your OData create operation\nHttpRequestMessage req = op.ToHttpRequestMessage(\"https://my_site.com/my_odata_endpoint\");\n```\n\n## Update Operation\nExample update operation on a Contact record with primary key `e23fa96e-46ac-4b06-bac1-02cf0fe636b8`:\n```\nODataOperation op = new ODataOperation();\nop.Operation = DataOperation.Update;\nop.Resource = \"Contact\";\nop.RecordIdentifier = \"e23fa96e-46ac-4b06-bac1-02cf0fe636b8\";\n\n//Create the payload that defines the properties that should be modified\nJObject jo = new JObject();\njo.Add(\"Address\", \"101 Main Street\");\nop.Payload = jo;\n\n//Create the HttpRequestMessage to send that contains your OData create operation\nHttpRequestMessage req = op.ToHttpRequestMessage(\"https://my_site.com/my_odata_endpoint\");\n```\n\n## Delete Operation\nExample deletion of a Contact record with primary key `e23fa96e-46ac-4b06-bac1-02cf0fe636b8`:\n```\nODataOperation op = new ODataOperation();\nop.Operation = DataOperation.Delete;\nop.Resource = \"Contact\";\nop.RecordIdentifier = \"e23fa96e-46ac-4b06-bac1-02cf0fe636b8\";\n\n//Create the HttpRequestMessage to send that contains your OData create operation\nHttpRequestMessage req = op.ToHttpRequestMessage(\"https://my_site.com/my_odata_endpoint\");\n```\n\n## Converting an OData HttpRequestMessage directly to SQL\nBack-end API's playing the server role are often the medium that directly accept an OData query via an HTTP request, execute the query against the SQL database, and return the appropriate response to the HTTP requestor. This library is designed to simplify this use case by directly handling the \"translation\" from an HttpRequestMessage to a SQL query. \n\nExample converting the `HttpRequestMessage` from the **Read** operation example above:\n```\n//\"req\" is an OData-formatted HttpRequestMessage that the server has received\n//Parsing the request:\nODataOperation op = ODataOperation.Parse(req);\n\n//Convert to SQL\nstring sql_query = op.ToSql();\nConsole.WriteLine(sql_query);\n\n//select top 25 FirstName,LastName from Contacts where Age \u003e= '47' and LastName = 'Smith' order by DateOfBirth desc\n```\n*Update* and *Delete* OData operations require the SQL table's primary key to be referenced. In these cases, you will need to pass the name of the primary key column to the `ToSql()` method.\n\nFor example, on the **Update** operation example from above:\n```\n//\"req\" is an OData-formatted HttpRequestMessage that the server has received\n//Parsing the request:\nODataOperation op = ODataOperation.Parse(req);\n\n//Convert to SQL\nstring sql_query = op.ToSql(\"ContactID\");\nConsole.WriteLine(sql_query);\n\n//update Contact set Address = '101 Main Street' where ContactID = 'e23fa96e-46ac-4b06-bac1-02cf0fe636b8'\n```\nIn the above example, `ContactID` is the primary key of the `Contact` table. If you do NOT specify the primary key but the `ToSql()` method requires it for a conversion, it will substitute the primary key with \"\u003cPRIMARY_KEY\u003e\":\n```\nupdate Contact set Address = '101 Main Street' where \u003cPRIMARY_KEY\u003e = 'e23fa96e-46ac-4b06-bac1-02cf0fe636b8'\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimhanewich%2Ftimhanewich.odata","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimhanewich%2Ftimhanewich.odata","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimhanewich%2Ftimhanewich.odata/lists"}