{"id":15101857,"url":"https://github.com/almightylks/queryprojection","last_synced_at":"2026-02-02T13:32:19.644Z","repository":{"id":231428965,"uuid":"781725194","full_name":"AlmightyLks/QueryProjection","owner":"AlmightyLks","description":"A simple way to create Select Expressions based on string input and lambda expressions","archived":false,"fork":false,"pushed_at":"2024-05-09T11:54:17.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T03:44:29.349Z","etag":null,"topics":["csharp","efcore","entity-framework-core","library","nuget","nuget-package","query","query-builder","sql"],"latest_commit_sha":null,"homepage":"","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/AlmightyLks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2024-04-03T23:17:48.000Z","updated_at":"2024-05-09T11:54:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"646ec760-1c58-4c03-8678-fe3b9455b6da","html_url":"https://github.com/AlmightyLks/QueryProjection","commit_stats":{"total_commits":20,"total_committers":2,"mean_commits":10.0,"dds":0.25,"last_synced_commit":"ffba3467994bbe6b09beced3c8f0d94b30fa21bb"},"previous_names":["almightylks/queryprojection"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlmightyLks%2FQueryProjection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlmightyLks%2FQueryProjection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlmightyLks%2FQueryProjection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlmightyLks%2FQueryProjection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlmightyLks","download_url":"https://codeload.github.com/AlmightyLks/QueryProjection/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247974595,"owners_count":21026742,"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":["csharp","efcore","entity-framework-core","library","nuget","nuget-package","query","query-builder","sql"],"created_at":"2024-09-25T18:42:07.623Z","updated_at":"2026-02-02T13:32:19.617Z","avatar_url":"https://github.com/AlmightyLks.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# QueryProjection\n\n---\n\n## NuGet\nhttps://www.nuget.org/packages/QueryProjection\n\n---\n\n# Why was this library created?\nWe needed a way to query only what was needed from the frontend, as our previous property filtering was done during json serialization, and resulted in unnecessarily long queries and big result sets as well as long-running queries. However, we did not want to introduce something more time-consuming and complex like GraphQL.  \nThis library allows projecting upon an EFCore database query to only select what is being asked for, without statically typing out the properties, but also offering lambda expressions for specific use cases.\n\n# How to use\nThis library essentially offers 1 primary extension method which is the following:\n```cs\npublic static class QueryProjectionExtension\n{\n    public static IQueryable\u003cobject\u003e Project\u003cT\u003e(this IQueryable\u003cT\u003e query, List\u003cIMapping\u003cT\u003e\u003e mappings, ParameterExpression? xParameter = null);\n}\n```\n\nAs you can see there is `IMapping\u003cT\u003e`. There is 2 structs implementing that interface. \n`FromToMapping` and `CustomMapping`.\n\n`FromToMapping` allows to simply map from the source object's property to a field name of your choice which will be in the resulting object.  \n`CustomMapping` allows you to specify a lambda expression, and acts as a Func\u003cTInput, TOutput\u003e. This allows to also include additional more complex ways to add fields to the result object.  \n\n## Usage\nCreate a list of mapping that you would like to apply.  \nFor example simply a to-from mapping with two strings.  \nA to-from mapping where the from string is nested within the object.  \nOr a more complex to-from mapping where from is a Func.  \n```cs\nvar fromToMapping = new List\u003cIMapping\u003cPerson\u003e\u003e()\n{\n    new FromToMapping\u003cPerson\u003e(to: \"PersonId\" , from: \"Id\"),\n    new FromToMapping\u003cPerson\u003e(to: \"FirstName\" , from: \"IdCard.FirstName\"),\n    new CustomMapping\u003cPerson, bool\u003e(to: \"HasJohnOnIdCard\", from: x =\u003e x.IdCard.FirstName.Contains(FirstName))\n};\n\nIQueryable\u003cobject\u003e query = _context.People.Project(fromToMapping);\nList\u003cobject\u003e results = query.ToList();\n```\n\n## What does the projection do?\nIt essentially dynamically creates anonymous objects within the assembly at runtime (like the compiler does at compile time) based on the each mapping's `To` Name and `From` Type.  \nAfter doing that, it essentially generates an Expression\u003cFunc\u003cT1, T2\u003e\u003e with something similar to an object initializer like from the above example, and calls `query.Select(expression)`.  \nThus:  \n```cs\nvar query = _context.People.Project(fromToMapping);\n// Identical to\nvar query = _context.People.Select(x =\u003e new\n{\n  PersonId = x.Id,\n  FirstName = x.IdCard.FirstName,\n  HasJohnOnIdCard = x.IdCard.FirstName.Contains(FirstName)\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falmightylks%2Fqueryprojection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falmightylks%2Fqueryprojection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falmightylks%2Fqueryprojection/lists"}