user7983964
user7983964

Reputation: 57

How can I get all records that matches a condition?

How can I get all records that matches a condition? I have the following code it throws exception when there are more than one result. How can I update my code to get all records.

protected override async Task<GetBillingByID_Action_Response> PerformActionAsync(GetBillingByID_Action_Request request)
{
    var Billing = await Context.Billings.Where(b => b.ProjectID == request.ID)
                                        .SingleOrDefaultAsync();
    var BillingDTO = Mapper.Map<BillingDTO>(Billing);

    return new GetBillingByID_Action_Response { Billing = BillingDTO };
}

I get this exception error:

System.InvalidOperationException: Enumerator failed to MoveNextAsync.

I also tried using ToListAsync()as:

var Billing = await Context.Billings.Where(b => b.ProjectID == request.ID).ToListAsync();

But I get this exception error

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: List`1 -> BillingDTO

Upvotes: 1

Views: 445

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39956

You should not use SingleOrDefaultAsync if you want to get all records, the ToListAsync is a correct choice based on what you need, however you need to map to a list of BillingDTO instead, since the Biling will be a list:

var BillingDTO = Mapper.Map<List<BillingDTO>>(Billing);

You need to change the following property as well:

return new GetBillingByID_Action_Response { Billing = BillingDTO }; 

The BillingDTO is a list now, so you need to change the Billing property in the GetBillingByID_Action_Response class to a list as well.

Upvotes: 1

Related Questions