I am wondering how do I change this statement to be async?
var findBarCode = context.Barcodes
.Where(x => x.Code == barcode)
.Select(x => x.Product).FirstOrDefault();
I don't see like any async where
statement I can use.
What about SingleAsync or FindAsync? Not sure about FirstOrDefault one
Do use await.
var findBarCode = await context.Barcodes
.Where(x => x.Code == barcode)
.SingleAsync(x => x.Product);
Another way (might silly as I have no access to VS at present):
var findBarCode = await context.Barcodes
.Where(x => x.Code == barcode)
.OrderBy(YOURCRITERIA)
.Take(1)
.Select(x => x.Product)
.ToListAsync();
There's an extension method called FirstOrDefaultAsync
in System.Data.Entity
:
using System.Data.Entity;
...
var findBarCode = await context.Barcodes
.Where(x => x.Code == barcode)
.Select(x => x.Product).FirstOrDefaultAsync();