简体   繁体   中英

Sequence contains no matching element C# Web Api

I did some searches around and don't like to remove the xml formatter as many have suggested, I want to pin point the issue and fix it appropriately, as in the future this api will need to output xml not just json.

With that out of the way, the issue is, with 1 of the api endpoints I am creating a list of transactions with some account info that I am trying to retrieve on an app. However, I am getting the following error in Postman.

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.

Within my code i am accessing a facade that gets the data for me, and assembling the result set in a bucket.

var Accounts = AccountsFacade.GetByID( resultSet.Select( t => t.AccountID ).Distinct() );

var Result = resultSet.Select( t => new ApiTransaction()
{
    ID = t.ID,
    Name = Accounts.Single( a => a.ID == t.ID ).Name,
    Surname = Accounts.Single( a => a.ID == t.ID ).Surname,
    Company = Accounts.Single( a => a.ID == t.ID ).Company,
    VatNumber = Accounts.Single( a => a.ID == t.ID ).VatNumber,
    Action = t.Action,
    Credit = t.Credit,
    Debit = t.Debit,
    Details = t.Details,
    Timestamp = t.Timestamp
} );

var resultBucket = new Bucket( )
{
    TotalResults = TransactionsCount,
    Count = resultSet.Count,
    Page = Page,
    TotalPages = TotalPages,
    Result = Result
};

var response = Request.CreateResponse( HttpStatusCode.OK, resultBucket, Configuration.Formatters.JsonFormatter );
response.Headers.Add( "Access-Control-Allow-Origin", "*" );
return response;

The return type of my endpoint is of 'HttpResponseMessage'

Debugging

I did some debugging and tried to identify the issue, but failed to find anything wrong with the response. The response is being set to the json format as expected and no errors are thrown within the controller or the application.

Any help is greatly appreciated.

The issue in my case was a stupid overlooked mistake

When selecting and building the new list with other info, the match was being made on account ID against transaction ID which was not getting any results due to my using of "Single".

I kept the Single but matched appropriately against t.AccountID

From

var Result = resultSet.Select( t => new ApiTransaction()
{
    ID = t.ID,
    Name = Accounts.Single( a => a.ID == t.ID ).Name,
    Surname = Accounts.Single( a => a.ID == t.ID ).Surname,
    Company = Accounts.Single( a => a.ID == t.ID ).Company,
    VatNumber = Accounts.Single( a => a.ID == t.ID ).VatNumber,
    Action = t.Action,
    Credit = t.Credit,
    Debit = t.Debit,
    Details = t.Details,
    Timestamp = t.Timestamp
} );

To

var Result = resultSet.Select( t => new ApiTransaction()
{
    ID = t.ID,
    Name = Accounts.Single( a => a.ID == t.AccountID ).Name,
    Surname = Accounts.Single( a => a.ID == t.AccountID ).Surname,
    Company = Accounts.Single( a => a.ID == t.AccountID ).Company,
    VatNumber = Accounts.Single( a => a.ID == t.AccountID ).VatNumber,
    Action = t.Action,
    Credit = t.Credit,
    Debit = t.Debit,
    Details = t.Details,
    Timestamp = t.Timestamp
} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM