Title: ASP.NET web api returning only first object in each object in a array
Tags: c#;asp.net-mvc;entity-framework;asp.net-core;asp.net-web-api
Question: startup.cs

```   services.AddMvc().AddJsonOptions(options =&gt;
        {
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
```

below is the sample response json returned by web api application when deployed to production. In localhost Iam getting data for  "lastHourData","lastHourBeforeData" but not in production.

```    [{
        "currentData": {
            "id": "213123123esdwq23d",
             "payload": [
                {
                    "key": "sdfsdf",
                    "value": "T"
                }
            ]
        },
        "lastHourData": null,
        "lastHourBeforeData": null
    },
    {
        "currentData": {
            "id": "sdfdsf",
             "payload": [
                {
                    "key": "gf",
                    "value": "T"
                }
            ]
        },
        "lastHourData": null,
        "lastHourBeforeData": null
    }]
```

below is the method, CurrentData,LastHourData and LastHourBeforeData each has 250 properities. 

```  public async Task<ComepletePanel&gt; GetCompletePanelData(string id)
    {
        var queryRequest = RequstBuilder(id);
        var result = await queryRequest;
        var currentResult = result.Items.Select(Map2).FirstOrDefault();

        var queryRequest2 = getlastHourDataForMasterPanelBuilder(id);
        var result2 = await queryRequest2;
        var currentResult2 = result2.Items.Select(Map2).FirstOrDefault();

        var queryRequest3 = getlastBeforHourDataForMasterPanelBuilder(id);
        var result3 = await queryRequest3;
        var currentResult3 = result3.Items.Select(Map2).FirstOrDefault();

        var completData = new ComepletePanel
        {
            CurrentData = currentResult,
            LastHourData = currentResult2,
            LastHourBeforeData = currentResult3
        };

        return completData;
    }
```

below is the controller method

```  public async Task<IEnumerable<ComepletePanel&gt;&gt; GetCustBoilersByCustId2(int id)
    {
        try
        {
            var recordData= await _context.table.ToListAsync();
            List<ComepletePanel&gt; resultData= new List<ComepletePanel&gt;();

            foreach (var b in recordData)
            {
                var CurrentResponse = await _getItem.GetCompletePanelData(b.id);
                resultData.Add(CurrentResponse);
            }

            return resultData;

        }
        catch (Exception ex)
        {
            _logger.LogError($"Failed to get all customers {ex}");
            return null;
        }
    }
```

Comment: it's the same table. just adding different datetime conditions for both of them. in localhost it's working and in the production it's not. same database and same table used for both the environments

Comment: If you are getting the first part of the data and not the rest  and it is working in localhost then it has to do with some sort of server settings/cutoffs. I will reduce the size of the data and/or console write/If 'null' to see if anything reaches your method.

Comment: Do you have data for LastHourData  and LastHourBeforeData  in PROD?


Here is another answer: You might need to serialize your object.  Please see this for your reference: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to

Comment for this answer: Is this correct  JsonSerializer.Serialize(completData); ? JsonSerializer doesn't have extension menthod called Serialize
Comment for this answer: Seems like you've configured the serialization options but you still need to call the serializer to serialize your object like this 

return JsonSerializer.Serialize(completData);
Comment for this answer: Do you have these in your using statement? 

using System.Text.Json;
using System.Text.Json.Serialization;
Comment for this answer: Ok I hope that helps.

Here is another answer: I think you should refactor your code to look more presentable and appealing to you like this. It might solve your problem.

Instead of you writing methods like ```getLastHourDataForMasterPanelBuilder(string id)``` and ```getlastHourDataForMasterPanelBuilder(string id)```, first those names look too long and then just summarize your algorithm in one method such that you just pass a time or a logic data or time argument to it then it gets the data based on time you want.

```public async Task<IEnumerable<CompletePanel&gt;&gt; GetCompleteData(string id params string[] args)
{
    List<QueryRequest&gt; requests = new List<QueryRequest&gt;();

    foreach (string argument in args)
    {
        var queryRequest = RequestBuilder(id);
        var result = await queryRequest;
        var knownResult = result.Items.Select(Map2).FirstOrDefault();
        requests.Add(knownResult);
    }

    return requests;
}
```

Then in your controller instead of performing a loop you just pass the id and time argument or logical argument as string to the method.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:12/05/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net-mvc><entity-framework><asp.net-core><asp.net-web-api>', 'Title': 'ASP.NET web api returning only first object in each object in a array', 'ViewCount': 359, 'id': 59201080, 'user_AboutMe': '<p>Apparently, this user prefers to keep an air of mystery about them.</p>\n', 'user_AccountId': 14296492.0, 'user_DisplayName': 'Raj', 'user_Reputation': 345.0, 'user_Views': 47.0, 'user_id': 10327018.0}}