Task>>' does not contain a definition for 'First'

asp.net-core ef-core-2.0 entity-framework entity-framework-core

Question

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 for First and no extension method First accepting a first argument of type Task<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

1
-1
9/3/2018 11:10:25 AM

Accepted Answer

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>>

1
9/3/2018 11:08:05 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow