What I'm doing so far is this:
Entities:
public class BaseEntity
{
public int Id { get; set; }
public DateTime CreateOn { get; set; }
}
public class Request : BaseEntity, IAggregateRoot
{
public Approval Approval { get; set; }
}
public class Approval // Value object
{
public bool? IsApproved { get; set; }
}
Repo:
public async Task<IReadOnlyList<Request>> GetAllAsync()
{
IQueryable<Request> requests = await _dbContext.Requests.ToListAsync();
IQueryable<Request> pending = requests.Where(r => r.Approval.IsApproved == null).OrderBy(r => r.CreateOn);
IQueryable<Request> processed = requests.Where(r => r.Approval.IsApproved != null).OrderByDescending(r => r.CreateOn);
return pending.Concat(processed).ToListAsync();
}
The problem I have is when I iterate through the result of GetAllAsync
and IsApproved
has a value, Approval
is set to null
. If I only return requests
without concatenating, it works as expected (the object is created, but the values within are null
).
I suspect the problem is with concatenating two queries. How can I rewrite what I have in a single query?
The requests should be grouped by processed and not processed (IsApproved == null
and IsApproved != null
), then ordered by CreatedOn
in different orders.
Would also greatly appreciate if someone can explain to me why after concatenating, Approval
is set to null
if IsApproved
has a value. Also, when I wait enough (~5s), while debugging through each iteration, it works as expected. Maybe there's a late reference that doesn't await
for some process to finish?
While writing this post, I did some testing. If I change IQueryable
to IEnumerable
it works as expected. After some more digging, I found this:
Queryable.Concat(IQueryable, IEnumerable) Method
Enumerable.Concat(IEnumerable, IEnumerable) Method
So I assume if I pass an IQueryable
, instead of IEnumerable
, to Queryable.Concat()
, then I lose some references? I'm so confused.