Search code examples
asp.net-mvclinqcode-first

How to put a column from a database table into a dynamic list?


For my Project I want to access the database especially one table which has the BauTeilId in it and putting all the BauTeilId into one List in the Controller


DefaultConnection:

"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=QualitätsKontrolleDB;Trusted_Connection=True;MultipleActiveResultSets=true"

Controller The String Code is from the HTML and consits of 6 digits

private ApplicationDbContext Context = new ApplicationDbContext();

        [HttpGet]
        public IActionResult StartPage(string Code)
        {
            Debug.WriteLine(Code);

            var list = Context.Result.All(c => c.BauTeilId);
            return View();
        }

Table used for this I only need the BauTeilId TypenID int PruefungenID int BildID int BauTeilID string Date DateTime xLabel string X int YLabel string Y int FehlerCode string Fehlername string FehlerGruppe1 string FehlerGruppe2 string Result int


Right now the output is not existent but it should be the whole column.


Solution

  • If to want to get only BauTeilId from table likeselect BauTeilId from table use

    (from t in Context.Result
     select new {
      t.BauTeilId
     }).toList();  // .toList(); is not necessary, Linq returns an `IEnumerable `
    

    That LINQ Statement,Hope you get point.