Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Select Expression using LINQ

$
0
0

I m trying to select and return certain columns form a table using the following code, instead of returning the columns that I select the query returns the whole table, what am I doing wrong?

        [HttpGet]
        public IEnumerable<IdState> GetState()
        {
            return _context.IdState;
        }
        [HttpGet("{id}")]
        public async Task<IActionResult> GetState([FromRoute] int id)
        {

            var L2EQuery = await (from i in _context.IdState
                           select new { Id = i.StaeId, i.text }).ToListAsync();
            return Ok(L2EQuery);
        }

Update

This is the model,

public class IdState
        {
            [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
            public int StaeId { set; get; }
            public int CouyId { set; get; }
            [Display(Name = "State"), Required]
            public string text { set; get; }
            public ICollection<PatReg> State { get; set; }
            public ICollection<IdCity> TblIdCity { get; set; }

        }

and the output and ajax call is,

[{"staeId": 1,"couyId": 2,"text": "California","state": null,"tblIdCity": null
}, {"staeId": 2,"couyId": 2,"text": "WA","state": null,"tblIdCity": null
}, {"staeId": 3,"couyId": 1,"text": "Ajloun","state": null,"tblIdCity": null
}, {"staeId": 4,"couyId": 1,"text": "Amman","state": null,"tblIdCity": null
}]
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}<script src="~/ArtCoreScripts/Pages/ClinicCore/PatientRegistration.js"></script><script>$(function () {$.ajax({
                type: "Get",
                url: "/api/Common",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    console.log(response);
                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });
        });</script>}

 


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>