locked
How to convert list to an object? RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I am querying entity as follows;

    var gameBankResultVM = await (context.GameBanks.Where(g => g.productCode == initiate.productCode)
                        .Where(l => l.referenceId == initiate.referenceId)
                        .Take(initiate.quantity)
                        .Select(g => new GameBankInitiationResponseVM()
                        {
                            referenceId = g.referenceId,
                            productCode = g.productCode,
                            quantity = g.quantity,
                            initiationResultCode = g.initiationResultCode,
                            validatedToken = g.validatedToken,
                            currency = g.currency,
                            estimateUnitPrice = g.estimateUnitPrice,
                            version = g.version,
                            ApplicationCode = g.ApplicationCode,
                            companyToken = g.companyToken
                            
                        })).ToListAsync();

    return Ok(gameBankResultVM);

    Sometimes <g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="15" data-gr-id="15">result</g> returns only one object and I don't want to return JSON array as follows. How can I eliminate this array brackets if there is only one result? Other than looping on the result and setting values into <g class="gr_ gr_166 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="166" data-gr-id="166">entitiy</g>.

    [
        {
            "referenceId": "48900000499",
            "productCode": "000000006446",
            "quantity": 1,
            "version": "V1",
            "initiationResultCode": "00",
            "companyToken": null,
            "validatedToken": null,
            "ApplicationCode": null,
            "currency": "TRY",
            "estimateUnitPrice": 10
        }
    ]

    Friday, May 3, 2019 6:34 PM

Answers

  • User1120430333 posted

    Sometimes result returns only one object and I don't want to return JSON array as follows. How can I eliminate this array brackets if there is only one result? Other than looping on the result and setting values into entitiy.

    So why can't you check the collection.count() = 1 and simply do  the below?

    var obj = collection.single(); //'and get the one object out of the collection 

    or

    var obj = collection(0); //use the collection indexer and get the object out of the collection.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 3, 2019 9:25 PM