async using linq
-
Hi, I have he following code which for the line "await GetSkorekortSkore(x.SkorekortID)" is giving the error message "the await operator can only be used within an async lambda expression. Consider marking this lambda expression with the async modifier". Any suggestion of how to make it work ?
return (from s in _db.SkoreKort
from k in _db.Klub
from b in _db.Bane
where (s.SkorekortID == SkorekortID &&
s.BaneKlubID == k.KlubID &&
s.BaneID == b.BaneID)
select new {
s.SkorekortID,
k.DGUKlubNr,
s.SkoreDato,
s.SpillerHCP,
k.KlubNavn,
b.BaneNavn,
b.TeeNavn,
b.HerreDame,
b.BaneID,
k.KlubID
}).AsQueryable()
.Select(x => new SkorekortDetailViewModel {
SkorekortID = x.SkorekortID,
DGUKlubNr = x.DGUKlubNr,
SkoreDato = x.SkoreDato,
SpillerHCP = x.SpillerHCP,
KlubNavn = x.KlubNavn,
BaneNavn = x.BaneNavn,
TeeNavn = x.TeeNavn,
HerreDame = x.HerreDame,
SkoreListe = await GetSkorekortSkore(x.SkorekortID)
}).ToListAsync();} public async Task<List<SkorekortSkore>> GetSkorekortSkore(int SkorekortID) { return await (from s in \_db.SkoreKortSkore where s.SkorekortID == SkorekortID select s).ToListAsync(); }
-
Hi, I have he following code which for the line "await GetSkorekortSkore(x.SkorekortID)" is giving the error message "the await operator can only be used within an async lambda expression. Consider marking this lambda expression with the async modifier". Any suggestion of how to make it work ?
return (from s in _db.SkoreKort
from k in _db.Klub
from b in _db.Bane
where (s.SkorekortID == SkorekortID &&
s.BaneKlubID == k.KlubID &&
s.BaneID == b.BaneID)
select new {
s.SkorekortID,
k.DGUKlubNr,
s.SkoreDato,
s.SpillerHCP,
k.KlubNavn,
b.BaneNavn,
b.TeeNavn,
b.HerreDame,
b.BaneID,
k.KlubID
}).AsQueryable()
.Select(x => new SkorekortDetailViewModel {
SkorekortID = x.SkorekortID,
DGUKlubNr = x.DGUKlubNr,
SkoreDato = x.SkoreDato,
SpillerHCP = x.SpillerHCP,
KlubNavn = x.KlubNavn,
BaneNavn = x.BaneNavn,
TeeNavn = x.TeeNavn,
HerreDame = x.HerreDame,
SkoreListe = await GetSkorekortSkore(x.SkorekortID)
}).ToListAsync();} public async Task<List<SkorekortSkore>> GetSkorekortSkore(int SkorekortID) { return await (from s in \_db.SkoreKortSkore where s.SkorekortID == SkorekortID select s).ToListAsync(); }
You haven't included the method definition for the top LINQ statement but that message is telling you that that method also needs to be async.
This space for rent
-
You haven't included the method definition for the top LINQ statement but that message is telling you that that method also needs to be async.
This space for rent
This is how it looks:
namespace Golf.Components
{
public class SkorekortDetailViewComponent : ViewComponent
{
public readonly GolfContext _db;public SkorekortDetailViewComponent(GolfContext context) { \_db = context; } public async Task InvokeAsync(int SkorekortID) { var items = await GetItemsAsync(SkorekortID); return View(items); } private async Task\> GetItemsAsync(int SkorekortID) { return (from s in \_db.SkoreKort from k in \_db.Klub from b in \_db.Bane where (s.SkorekortID == SkorekortID && s.BaneKlubID == k.KlubID && s.BaneID == b.BaneID) select new { s.SkorekortID, k.DGUKlubNr, s.SkoreDato, s.SpillerHCP, k.KlubNavn, b.BaneNavn, b.TeeNavn, b.HerreDame, b.BaneID, k.KlubID }).AsQueryable() .Select(x => new SkorekortDetailViewModel { SkorekortID = x.SkorekortID, DGUKlubNr = x.DGUKlubNr, SkoreDato = x.SkoreDato, SpillerHCP = x.SpillerHCP, KlubNavn = x.KlubNavn, BaneNavn = x.BaneNavn, TeeNavn = x.TeeNavn, HerreDame = x.HerreDame, SkoreListe = await GetSkorekortSkore(x.SkorekortID) }).ToListAsync(); } public async Task\> GetSkorekortSkore(int SkorekortID) { return await (from s in \_db.SkoreKortSkore where s.SkorekortID == SkorekortID select s).ToListAsync(); }
-
This is how it looks:
namespace Golf.Components
{
public class SkorekortDetailViewComponent : ViewComponent
{
public readonly GolfContext _db;public SkorekortDetailViewComponent(GolfContext context) { \_db = context; } public async Task InvokeAsync(int SkorekortID) { var items = await GetItemsAsync(SkorekortID); return View(items); } private async Task\> GetItemsAsync(int SkorekortID) { return (from s in \_db.SkoreKort from k in \_db.Klub from b in \_db.Bane where (s.SkorekortID == SkorekortID && s.BaneKlubID == k.KlubID && s.BaneID == b.BaneID) select new { s.SkorekortID, k.DGUKlubNr, s.SkoreDato, s.SpillerHCP, k.KlubNavn, b.BaneNavn, b.TeeNavn, b.HerreDame, b.BaneID, k.KlubID }).AsQueryable() .Select(x => new SkorekortDetailViewModel { SkorekortID = x.SkorekortID, DGUKlubNr = x.DGUKlubNr, SkoreDato = x.SkoreDato, SpillerHCP = x.SpillerHCP, KlubNavn = x.KlubNavn, BaneNavn = x.BaneNavn, TeeNavn = x.TeeNavn, HerreDame = x.HerreDame, SkoreListe = await GetSkorekortSkore(x.SkorekortID) }).ToListAsync(); } public async Task\> GetSkorekortSkore(int SkorekortID) { return await (from s in \_db.SkoreKortSkore where s.SkorekortID == SkorekortID select s).ToListAsync(); }
Try
return await (from s in _db.SkoreKort
You're attempting to return an async operation without actually specifying that it's async.
This space for rent
-
Try
return await (from s in _db.SkoreKort
You're attempting to return an async operation without actually specifying that it's async.
This space for rent
It didn't change the error, it is still the same. The error message suggest to add async in front of x => new SkorekortDetailViewModel as
}).AsQueryable()
.Select(async x => new SkorekortDetailViewModel {
SkorekortID = x.SkorekortID,
DGUKlubNr = x.DGUKlubNr,
SkoreDato = x.SkoreDato,
SpillerHCP = x.SpillerHCP,
KlubNavn = x.KlubNavn,
BaneNavn = x.BaneNavn,
TeeNavn = x.TeeNavn,
HerreDame = x.HerreDame,
SkoreListe = await GetSkorekortSkore(x.SkorekortID),
HulListe = await GetHuller(x.BaneID),
BaneListe = await GetBane(x.KlubID)
}).ToListAsync();but that just shows all the linq as in error.
-
Hi, I have he following code which for the line "await GetSkorekortSkore(x.SkorekortID)" is giving the error message "the await operator can only be used within an async lambda expression. Consider marking this lambda expression with the async modifier". Any suggestion of how to make it work ?
return (from s in _db.SkoreKort
from k in _db.Klub
from b in _db.Bane
where (s.SkorekortID == SkorekortID &&
s.BaneKlubID == k.KlubID &&
s.BaneID == b.BaneID)
select new {
s.SkorekortID,
k.DGUKlubNr,
s.SkoreDato,
s.SpillerHCP,
k.KlubNavn,
b.BaneNavn,
b.TeeNavn,
b.HerreDame,
b.BaneID,
k.KlubID
}).AsQueryable()
.Select(x => new SkorekortDetailViewModel {
SkorekortID = x.SkorekortID,
DGUKlubNr = x.DGUKlubNr,
SkoreDato = x.SkoreDato,
SpillerHCP = x.SpillerHCP,
KlubNavn = x.KlubNavn,
BaneNavn = x.BaneNavn,
TeeNavn = x.TeeNavn,
HerreDame = x.HerreDame,
SkoreListe = await GetSkorekortSkore(x.SkorekortID)
}).ToListAsync();} public async Task<List<SkorekortSkore>> GetSkorekortSkore(int SkorekortID) { return await (from s in \_db.SkoreKortSkore where s.SkorekortID == SkorekortID select s).ToListAsync(); }
Were any parts actually working? I would classify this as "unmaintainable"; and getting it to actually run would be bad karma. You need to start with (working) "sub queries" that can be combined to arrive at the desired result; and where intermediate results can be checked / dumped if needed. (Even the "SQL query plan optimizer" needs help sometimes).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
Were any parts actually working? I would classify this as "unmaintainable"; and getting it to actually run would be bad karma. You need to start with (working) "sub queries" that can be combined to arrive at the desired result; and where intermediate results can be checked / dumped if needed. (Even the "SQL query plan optimizer" needs help sometimes).
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
It works fine without the line "SkoreListe = await GetSkorekortSkore(x.SkorekortID)".
I'm not sure if it can work the way i thought it should work, so I have changed it to this instead which works:
private async Task\> GetItemsAsync(int SkorekortID) { List skorekortdetail = await (from s in \_db.SkoreKort from k in \_db.Klub from b in \_db.Bane where (s.SkorekortID == SkorekortID && s.BaneKlubID == k.KlubID && s.BaneID == b.BaneID) select new { s.SkorekortID, k.DGUKlubNr, s.SkoreDato, s.SpillerHCP, k.KlubNavn, b.BaneNavn, b.TeeNavn, b.HerreDame, b.BaneID, k.KlubID }).AsQueryable() .Select(x => new SkorekortDetailViewModel { SkorekortID = x.SkorekortID, DGUKlubNr = x.DGUKlubNr, SkoreDato = x.SkoreDato, SpillerHCP = x.SpillerHCP, KlubNavn = x.KlubNavn, BaneNavn = x.BaneNavn, TeeNavn = x.TeeNavn, HerreDame = x.HerreDame, BaneID = x.BaneID, KlubID = x.KlubID }).ToListAsync(); foreach(var skorekort in skorekortdetail) { skorekort.SkoreListe = await GetSkorekortSkoreAsync(skorekort.SkorekortID); skorekort.HulListe = await GetHullerAsync(skorekort.BaneID); skorekort.BaneListe = await GetBaneAsync(skorekort.KlubID); } return skorekortdetail; }