I am using System.Linq.Dynamic.Core v 1.2.24 to create a dynamic select statement when querying SQL Server via Entity Framework. This is sitting behind a GraphQL API and the intention is to make the database query as efficient as possible - only query the database for the requested columns. I have the projection working for the type being enumerated but that type has a child property which also needs projection.
I have created a simple Fiddle to demonstrate this .
https://dotnetfiddle.net/nvU7DB
In the example I am querying a Person collection and Person has a property Address. I am successfully projecting the Person properties but can't figure out how to project the Address properties. I have not been able to find any examples of this.
This dynamic linq string will bring back all properties of Address: "new Person(Id, FirstName, Address)" but I want to do a projection on Address to just populate Id and City for example.
I was thinking the string might look like this "new Person(Id, FirstName, new Address(Id, City)" but this results in "Unhandled exception. Type 'Address' not found (at index 37)"
Without the 'new': "new Person(Id, FirstName, Address(Id, City))" which results in "Unhandled exception. No property or field 'City' exists in type 'Person' (at index 38)".
To clarify what I'm trying to do, this is what the projection would look like in C# code:
var projectedPeople = people.Select(
e => new Person() {
Id = e.Id,
FirstName = e.FirstName,
Address = new Address(){
Id = e.Address.Id,
City = e.Address.City
}
});
Any help or insights would be appreciated.
For anyone picking this up later, the solution was as follows