I have three tables that I am using.

Equipment - Contains a boolean Active field and a model id Makes - simply just has a description of the make Models - contains a make_id

I am loading a page that displays make objects and if any of these objects are active.

Makes can have many models.

One result Might have is the Make Dell with 5 models. 4 of these models might be active which is what I want to also display.

My current query is this:

foreach (var l in context.Makes)
                {
                    SomeViewModel search = new SomeViewModel();
                    search.ActiveUnits = context.Equipments.Include(x => x.Model_ID).Select(x => new { IsActive = x.Connection.IsActive }).Where(q => q.IsActive).Count();
                    SomeViewModel.Add(search);
                }

This query currently takes minutes to return results back. There are up to 10000 records.

Is there a better way I can write this to better performance?

Related posts

Recent Viewed