I want to separate filtering, count and selection operation from each but I get an error
The entity or complex type 'Parent' cannot be constructed in a LINQ to Entities query.
from the GetData
function.
I know the problem but I couldn't solve it. What is the right architecture to separate these kinds of complex queries?
string string name="Any Name";
int childId = 4;
int page = 2, maxDispItem = 10;
public IQueryable<Parent> GetFilteredData()
{
GetData().Where(o=>o.Name==name).SelectMany(o=>o.Children).
Where(o=>o.ID>childId).Select(o=>o.Parent).AsQueryable();
}
public IQueryable<Parent> GetData()
{
return context.Set<Parent>.AsNoTracking().Select(o => new { o.ID,o.Name,
Childs = o.Children.Select(t => new { t.ID }) }).
Select(o => new Parent { ID = o.ID, Name = o.Name,
Children = o.Childs.Select(t => new Child { ID = o.ID }).ToList()}).
AsQueryable();
}
public int GetDataCount()
{
return GetFilteredData().SelectMany(o=>o.Children).Count();
}
public List<Parent> GetModelData()
{
return GetFilteredData().OrderBy(o => o.ID).
Skip(maxDispItem * (page -1)).Take(maxDispItem).ToList();
}