var user = await context.Customers
.Include(x => x.CustomerPreferences)
.ThenInclude(up => up.CustomerNations)
.Where(x => x.Id == userId).FirstOrDefaultAsync(cancellationToken);
CustomerNations Entity has
public Customer Customer { get; set; } = null!;
public virtual IList<CustomerNation> CustomerNations { get; set; } = new List<CustomerNation>();
I have no record in CustomerNation table. However, according to this code, there is one record in CustomerNations. When I observe it during debug, Result view shows 0 results.
var preferences= user.preferences
.ToList();
if (preferences.Any())
{
var customerNations= user.CustomerPreferences
.Select(x => x.CustomerNations);
if (customerNations.Count() != 0) //Both, Any() and Count shows there is one record. I check dropdown and Result view has 0 results.
{
foreach (var nation in customerNations)
{
context.Remove(counterParty);
}
}
}
Count is 1 but result view says diffent
CodePudding user response:
Enumerable.Select<TSource,TResult>
returns an IEnumerable<TResult>
. In this case, since CustomerPreferences.CustomerNations
is of type IList<CustomerNation>
, user.CustomerPreferences.Select(x => x.CustomerNations)
is returning an IEnumerable<IList<CustomerNation>>
. So, your customerNations
probably has one item which itself is an empty IList<CustomerNation>
.
If you want to flatten all of the CustomerNation
instances into a IEnumerable<CustomerNation>
you should use Enumerable.SelectMany
.
var customerNations = user.CustomerPreferences.SelectMany(x => x.CustomerNations)