0

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.

Community
  • 1
  • 1
vixero
  • 514
  • 1
  • 9
  • 21
  • 1
    Shouldn't `requests` be of type `IList`? Then `pending` and `processed` would be `IEnumerable` and the last line would be `ToList()` instead of `ToListAsync()`. – ckuri Mar 21 '19 at 16:04
  • Have you tried to make your `pending` and `processed` into a List using `.ToList()` and then doing `pending.Concat(processed).ToList`, to make sure there is nothing else going on – Vikhram Mar 21 '19 at 16:23
  • Wouldn't that have an impact on performance? Somewhere else in the code I'm doing another `.Where(...)` so it would be something like `Where(...).ToList().Where(...)`. – vixero Mar 22 '19 at 08:16
  • As per this answer (https://stackoverflow.com/a/2876655/2949081), I want to do LINQ-to-SQL. – vixero Mar 22 '19 at 08:23

0 Answers0