I have the following two classes:
public Part {
public string PartNumber {get; set;}
public string Description {get; set;}
public List<Warehouse> Warehouses {get; set;}
}
public Warehouse {
public string PartNumber {get; set;}
public string WarehouseName {get; set;}
public int Quantity {get; set;}
public int ReorderPoint {get; set;}
}
Using Entity Framework Core 2.0 I have associated these using a one to many relationship. Using Dynamic Linq Core I'm trying to create a query that returns the PartNumber, Description, and the list of all associated Warehouses for a particular part where the only property in the Warehouses list is WarehouseName ideally like this:
List<string> fields = new List<string> {"PartNumber", "Description", "Warehouses.WarehouseName"};
var _dataSet = dbContext.Parts.Include(x => x.Warehouses);
var data = _dataSet.Where("PartNumber = \"Part1234\"").Select("new (" + String.Join(",", fields) + ")").ToDynamicArray();
But I receive this error: "No property or field 'Warehouse' exists in type 'List`1'". If I do something like this it works fine:
var data = _dataSet.Where("PartNumber = \"Part1234\"").Select(x => new Part
{
PartNumber = x.PartNumber,
Description = x.Description,
Warehouses = x.Warehouses.Select(y => new Warehouse { Warehouse = y.Warehouse }).ToList()
}).Single();
The problem is that I would like it to be dynamic so that the user can just pass in a list of fields from the Part and Warehouse class that they want to get without having to modify the select to build it for those specific fields.