简体   繁体   中英

OData service Out of memory exception

I have an OData service build under Web Api V2. I have a controller with a Linq query to return the data from a table. This is the controller query:

    [EnableQuery]
    public IQueryable<ImportContracts_ImportContracts> Get()
    {
        IQueryable<ImportContracts_ImportContracts> query = new List<ImportContracts_ImportContracts>().AsQueryable();

        query = (from BRRIMPORTCONTRACTS imcon in db.BRRIMPORTCONTRACTS.Where(x => x.ZZSTATE == 0)
                 select new
                 {
                     ReferenceDateData = imcon.ReferenceDate,
                     ImportationDate = imcon.ImportationDate,
                     LastImportation = imcon.LastImportation
                 }).ToList().Select(x => new ImportContracts_ImportContracts
                 {
                     ReferenceDateData = x.ReferenceDateData,
                     ImportationDate = x.ImportationDate,
                     LastImportation = x.LastImportation
                 }).AsQueryable();
        return query;
    }

My table BRRMIMPORTCONTRACTS have 1million records. When I get here, I get an System.OutOfMemoryException.

I know maybe this isn't the best way to query the entity but I just want a workaround for this.

I've tried [EnableQuery(PageSize=10)] hint but with no success. I think the problem is in the query itself.

Thanks in advance. Filipe

Does this make a difference?

[EnableQuery]
public IQueryable<ImportContracts_ImportContracts> Get()
{
    IQueryable<ImportContracts_ImportContracts> query = new List<ImportContracts_ImportContracts>().AsQueryable();

     query = (from BRRIMPORTCONTRACTS imcon in db.BRRIMPORTCONTRACTS.Where(x => x.ZZSTATE == 0)
         .Select(x => new ImportContracts_ImportContracts
         {
             ReferenceDateData = x.ReferenceDate,
             ImportationDate = x.ImportationDate,
             LastImportation = x.LastImportation
         });
    return query;
}

Note that your ToList() is gone, so an IQueryable is returned. Does that help?

Can you apply the query by yourself, for example

public IQueryable<ImportContracts_ImportContracts> Get(ODataQueryOptions<..> options)
{
    IQueryable<ImportContracts_ImportContracts> query = new List<ImportContracts_ImportContracts>().AsQueryable();

     // use the options to apply
     ...   

     return query;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM