简体   繁体   English

序列不包含匹配元素 C# Web Api

[英]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. 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.顺便说一句,问题是,使用 api 端点中的 1 个,我正在创建一个交易列表,其中包含我试图在应用程序上检索的一些帐户信息。 However, I am getting the following error in Postman.但是,我在 Postman 中收到以下错误。

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; “ObjectContent`1”类型无法序列化内容类型“application/json”的响应正文; charset=utf-8'.字符集=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'我的端点的返回类型是“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.响应按预期设置为 json 格式,并且在 controller 或应用程序中不会引发任何错误。

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".在使用其他信息选择和构建新列表时,帐户 ID 与交易 ID 的匹配是由于我使用“单一”而没有得到任何结果。

I kept the Single but matched appropriately against t.AccountID我保留了 Single 但与 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
} );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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