I have a general configuration table where settings are stored as string records. Table has <5 records. I need to return data from this table into a json result I've done this way
return Ok(new
{
SNAME = db.PAR.First(p => p.ID == 1).VAL.Trim(),
PART = db.PAR.First(p => p.ID == 2).VAL.Trim(),
NRZMAXEDIT = db.PAR.First(p => p.ID == 3).VAL,
.......
});
the code could be improved, so I've rewritten it to:
var res = db.PAR.ToList();
return Ok(new
{ SNAME = res.First(p => p.ID == 1).VAL,
PART =res.First(p => p.ID == 2).VAL,
NRZMAXEDIT = res.First(p => p.ID == 3).VAL,
....
});
It works but if I use async var res = db.PAR.ToListAsync();
, I am getting an error:
Task<List<<anonymous type: int ID, string VAL>>>
does not contain a definition forFirst
and no extension methodFirst
accepting a first argument of typeTask<List<<anonymous type: int ID, string VAL>>>
could be found (are you missing a using directive or an assembly reference?)
on every line where I use query like that:
res.First(p => p.ID == 1).VAL,
thanks
You need to use await
keyword before db.PAR.ToListAsync()
like below.
var res = await db.PAR.ToListAsync();
Note: The return type of ToListAsync()
is Task<List<TSource>>