0

I need to get the data from my database using a web api. Below is the code on how I implement it.

[HttpGet]
[Authorize]
[Route("api/getproperties")]
public async Task<List<Property>> GetProperties()
{
    using(var db = new ApplicationDbContext())
    {
        var properties = await (from p in db.Properties
                        join pt in db.PropertyTypes
                        on p.PropertyTypeId equals pt.PropertyTypeId
                        select new
                        {
                            PropertyId = p.PropertyId,
                            PropertyName = p.PropertyName,
                            Owner = p.Owner,
                            Cluster = p.Cluster,
                            PropertyNumber = p.PropertyNumber,
                            RegionCode = p.RegionCode,
                            ProvinceCode = p.ProvinceCode,
                            MunicipalCode = p.MunicipalCode,
                            BarangayCode = p.BarangayCode,
                            DateAdded = p.DateAdded,
                            DateModified = p.DateModified,
                            PropertyTypeId = p.PropertyTypeId,
                            PropertyType = p.PropertyType,
                            Type = pt.Type
                        }
                                ).ToList()
                                .Select(x => new Property
                                {
                                    PropertyId = x.PropertyId,
                                    PropertyName = x.PropertyName,
                                    Owner = x.Owner,
                                    Cluster = x.Cluster,
                                    PropertyNumber = x.PropertyNumber,
                                    RegionCode = x.RegionCode,
                                    ProvinceCode = x.ProvinceCode,
                                    MunicipalCode = x.MunicipalCode,
                                    BarangayCode = x.BarangayCode,
                                    DateAdded = x.DateAdded,
                                    DateModified = x.DateModified,
                                    PropertyTypeId = x.PropertyTypeId,
                                    PropertyType = x.PropertyType,
                                    Type = x.Type
                                }).ToListAsync();

        return properties;
    }
}

If I do not use the "async Task>" and remove the ".ToListAsync()" and "await" at the beginning, I do not get the errors. But with it, the function is not going to be Asynchronous anymore. Can you please show me how to this right? Thank you.

6
  • 1
    What errors? Please include as much information as possible. Commented Nov 4, 2018 at 5:42
  • Why are you doing another Select? Commented Nov 4, 2018 at 5:56
  • On the basis of @Peter's question being ignored for 36 minutes, I'm voting to close this question. Commented Nov 4, 2018 at 6:20
  • That is how it was implemented on this tutorial "c-sharpcorner.com/UploadFile/rahul4_saxena/…" Commented Nov 4, 2018 at 7:29
  • @PeterBons Sorry for the late reply. It has something to do with ToListAsync is not found. Commented Nov 4, 2018 at 7:31

1 Answer 1

2

When you use ToList() on IQueryable then asynchrony disappears since it loads data into memory synchronously.

Then you try to invoke ToListAsync on IEnumerable instead of IQueryable.

Your code should look like this:

public async Task<List<Property>> GetProperties()
{
    using(var db = new ApplicationDbContext())
    {
        var properties = await (from p in db.Properties
                          join pt in db.PropertyTypes
                          on p.PropertyTypeId equals pt.PropertyTypeId
                          select new Property()
                          {
                              PropertyId = p.PropertyId,
                              PropertyName = p.PropertyName,
                              Owner = p.Owner,
                              Cluster = p.Cluster,
                              PropertyNumber = p.PropertyNumber,
                              RegionCode = p.RegionCode,
                              ProvinceCode = p.ProvinceCode,
                              MunicipalCode = p.MunicipalCode,
                              BarangayCode = p.BarangayCode,
                              DateAdded = p.DateAdded,
                              DateModified = p.DateModified,
                              PropertyTypeId = p.PropertyTypeId,
                              PropertyType = p.PropertyType,
                              Type = pt.Type
                          }).ToListAsync();

        return properties;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank ypu for the answer Sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.