Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Making effective db calls (reducing number of trips to db)?

$
0
0

I have some code that sends many individual calls to the db in order to populate data in one viewmodel.

I have one Company entity. It has Meetings and Plans as collections. I don't want to include all meetings and plans in the db call because they need to be sorted. I want the sorting to be done in the db and then return results:

            var tid = GetDateAndTimeService.GetCurrentTime();

            var company = await _context.Companies
                .Include(d => d.CompanyDocuments).ThenInclude(d => d.Document)
                .AsNoTracking().FirstOrDefaultAsync(i => i.ID == Id);
            company.Meetings = await _context.Meetings.Where(p => p.Protokoll == false).Where(d => d.Dato.Date >=
            tid.Date).Where(i => i.CompanyID == Id)
                .OrderBy(dato => dato.Dato).Take(1).AsNoTracking().ToListAsync();
            company.Plans = await _context.Plans.Where(i => i.CompanyID == Id)
                .Where(n => n.Dato.Date >= tid.Date).OrderBy(m => m.Dato).Take(4).AsNoTracking().ToListAsync();
            return company;

As you see this results in 3 calls to the database. But if I try to filter on the Include() calls it does not work. So I cant do this: _context.Companies.Include(m =>m.meetings).Where(p => p.Protokoll == false) as the where method only works on the company entity here.

Can someone help me with this in some capacity? Any advice and help would be very appreciated!


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>