Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8862645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:46:39+00:00 2026-06-14T15:46:39+00:00

For a project we have had to use existing tables with Entity framework. This

  • 0

For a project we have had to use existing tables with Entity framework. This resolved in 1 large linq query for the search function. Currently this function takes like 3 seconds for 10 results.

The question is if someone knows points to improve this linq?

This is the linq query:

var result = (from o in db.Objects
                                  join omsc in db.Omschrijvingen on o.Omschrijving_Identifier equals omsc.Identifier
                                  join a in db.Adressen on o.Adres_Id equals a.Adres_Id
                                  join oa in db.Object_Abonnement on o.Object_Id equals oa.Object_ID
                                  join oaf in db.Object_Afbeeldingen on o.Object_Id equals oaf.Object_Id
                                  where o.Enabled && oaf.StandaardAfbeelding == true &&
                                        (!iType.Any() || iType.Contains(o.Type_Id)) &&
                                        (iProvince < 1 || a.Provincie_Id == iProvince) &&
                                        (iDepartement < 1 || a.Departement_Id == iDepartement) &&
                                        o.Object_Logs.All(
                                            ol => ol.Log_Type != (int) ApplicationDefinitions.LogTypes.Deleted) &&
                                        (oa.Betaald && oa.Tot >= DateTime.Now) &&
                                        (!kenmerken.Any() ||
                                         db.Object_Kenmerken.Count(
                                             ken =>
                                             kenmerken.Contains(ken.Kenmerk_Id) && ken.Object_Id == o.Object_Id &&
                                             ken.Waarde.ToUpper() != "FALSE" && ken.Waarde != "0") == kenmerken.Count()) &&
                                        (search.Length < 1 || omsc.Naam.ToLower().Contains(search.ToLower()) ||
                                         omsc.Omschrijving.ToLower().Contains(search.ToLower()) ||
                                         a.Woonplaats.ToLower().Contains(search.ToLower()))
                                  select new FullObject
                                             {
                                                 Object_Id = o.Object_Id,
                                                 Adres_Id = a.Adres_Id,
                                                 DepartmentId = a.Departement_Id,
                                                 Department = (a.Departementen != null) ? a.Departementen.Naam : "",
                                                 ProviciesId = a.Provincie_Id,
                                                 Provicie = (a.Provicies != null) ? a.Provicies.Naam : "",
                                                 Contact_Id = o.Contact_Id,
                                                 Naam = omsc.Naam,
                                                 Omschrijving = omsc.Omschrijving,
                                                 Prijs =
                                                     (((db.Prijs_Periode.Where(pp => pp.Object_Id == o.Object_Id).Min(
                                                         pp => pp.Prijs_Dag) > 0)
                                                           ? db.Prijs_Periode.Where(pp => pp.Object_Id == o.Object_Id).
                                                                 Min(pp => pp.Prijs_Dag)
                                                           : db.Prijs_Periode.Where(pp => pp.Object_Id == o.Object_Id).
                                                                 Min(pp => pp.Prijs_Week)) > 0)
                                                         ? (db.Prijs_Periode.Where(pp => pp.Object_Id == o.Object_Id).
                                                                Min
                                                                (pp => pp.Prijs_Dag) > 0)
                                                               ? db.Prijs_Periode
                                                                     .Where(
                                                                         pp => pp.Object_Id == o.Object_Id)
                                                                     .Min(pp => pp.Prijs_Dag)
                                                               : db.Prijs_Periode
                                                                     .Where(
                                                                         pp => pp.Object_Id == o.Object_Id)
                                                                     .Min(pp => pp.Prijs_Week/7)
                                                         : (db.Prijs_Periode
                                                                .Where(pp => pp.Object_Id == o.Object_Id)
                                                                .Min(pp => pp.Prijs_Weekend) > 0)
                                                               ? db.Prijs_Periode
                                                                     .Where(
                                                                         pp => pp.Object_Id == o.Object_Id)
                                                                     .Min(pp => pp.Prijs_Weekend/3)
                                                               : (db.Prijs_Periode
                                                                      .Where(
                                                                          pp => pp.Object_Id == o.Object_Id)
                                                                      .Min(pp => pp.Prijs_Midweek) > 0)
                                                                     ? db.Prijs_Periode
                                                                           .Where(
                                                                               pp =>
                                                                               pp.Object_Id == o.Object_Id)
                                                                           .Min(pp => pp.Prijs_Midweek/14)
                                                                     : (db.Prijs_Periode
                                                                            .Where(
                                                                                pp =>
                                                                                pp.Object_Id == o.Object_Id)
                                                                            .Min(pp => pp.Prijs_Langweekend) >
                                                                        0)
                                                                           ? db.Prijs_Periode
                                                                                 .Where(
                                                                                     pp =>
                                                                                     pp.Object_Id == o.Object_Id)
                                                                                 .Min(
                                                                                     pp =>
                                                                                     pp.Prijs_Langweekend)
                                                                           : 0,
                                                 SPrijs = (((db.Prijs_Periode
                                                                 .Where(
                                                                     pp =>
                                                                     pp.Object_Id == o.Object_Id &&
                                                                     pp.Aanbieding == false)
                                                                 .Min(pp => pp.Prijs_Dag) > 0)
                                                                ? db.Prijs_Periode
                                                                      .Where(
                                                                          pp =>
                                                                          pp.Object_Id == o.Object_Id &&
                                                                          pp.Aanbieding == false)
                                                                      .Min(pp => pp.Prijs_Dag)
                                                                : db.Prijs_Periode
                                                                      .Where(
                                                                          pp =>
                                                                          pp.Object_Id == o.Object_Id &&
                                                                          pp.Aanbieding == false)
                                                                      .Min(pp => pp.Prijs_Week)) > 0)
                                                              ? (db.Prijs_Periode
                                                                     .Where(
                                                                         pp =>
                                                                         pp.Object_Id == o.Object_Id &&
                                                                         pp.Aanbieding == false)
                                                                     .Min(pp => pp.Prijs_Dag) > 0)
                                                                    ? db.Prijs_Periode
                                                                          .Where(
                                                                              pp =>
                                                                              pp.Object_Id == o.Object_Id &&
                                                                              pp.Aanbieding == false)
                                                                          .Min(pp => pp.Prijs_Dag)
                                                                    : db.Prijs_Periode
                                                                          .Where(
                                                                              pp =>
                                                                              pp.Object_Id == o.Object_Id &&
                                                                              pp.Aanbieding == false)
                                                                          .Min(pp => pp.Prijs_Week/7)
                                                              : (db.Prijs_Periode
                                                                     .Where(
                                                                         pp =>
                                                                         pp.Object_Id == o.Object_Id &&
                                                                         pp.Aanbieding == false)
                                                                     .Min(pp => pp.Prijs_Weekend) > 0)
                                                                    ? db.Prijs_Periode
                                                                          .Where(
                                                                              pp =>
                                                                              pp.Object_Id == o.Object_Id &&
                                                                              pp.Aanbieding == false)
                                                                          .Min(pp => pp.Prijs_Weekend/3)
                                                                    : (db.Prijs_Periode
                                                                           .Where(
                                                                               pp =>
                                                                               pp.Object_Id == o.Object_Id &&
                                                                               pp.Aanbieding == false)
                                                                           .Min(pp => pp.Prijs_Midweek) > 0)
                                                                          ? db.Prijs_Periode
                                                                                .Where(
                                                                                    pp =>
                                                                                    pp.Object_Id == o.Object_Id &&
                                                                                    pp.Aanbieding == false)
                                                                                .Min(pp => pp.Prijs_Midweek/14)
                                                                          : (db.Prijs_Periode
                                                                                 .Where(
                                                                                     pp =>
                                                                                     pp.Object_Id == o.Object_Id &&
                                                                                     pp.Aanbieding == false)
                                                                                 .Min(pp => pp.Prijs_Langweekend) >
                                                                             0)
                                                                                ? db.Prijs_Periode
                                                                                      .Where(
                                                                                          pp =>
                                                                                          pp.Object_Id == o.Object_Id &&
                                                                                          pp.Aanbieding == false)
                                                                                      .Min(
                                                                                          pp =>
                                                                                          pp.Prijs_Langweekend)
                                                                                : 0,
                                                 Personen =
                                                     (db.Object_Kenmerken.Any(
                                                         iok =>
                                                         iok.Kenmerk_Id == iAantalPersonenId &&
                                                         iok.Object_Id == o.Object_Id))
                                                         ? db.Object_Kenmerken.Where(
                                                             iok =>
                                                             iok.Kenmerk_Id == iAantalPersonenId &&
                                                             iok.Object_Id == o.Object_Id).Select(
                                                                 iok => EntitiesFunctions.ParseInt(iok.Waarde)).
                                                               FirstOrDefault()
                                                         : 0,
                                                 Slaapkamers =
                                                     (db.Object_Kenmerken.Any(
                                                         iok =>
                                                         iok.Kenmerk_Id == iAantalSlaapkamersId &&
                                                         iok.Object_Id == o.Object_Id))
                                                         ? db.Object_Kenmerken.Where(
                                                             iok =>
                                                             iok.Kenmerk_Id == iAantalSlaapkamersId &&
                                                             iok.Object_Id == o.Object_Id).Select(
                                                                 iok => EntitiesFunctions.ParseInt(iok.Waarde)).
                                                               FirstOrDefault()
                                                         : 0,
                                                 UrlKey = omsc.UrlKey,
                                                 Title = omsc.Title,
                                                 Plaats = a.Woonplaats,
                                                 Enabled = o.Enabled,
                                                 GoogleMap_Url = o.GoogleMap_Url,
                                                 InkoopPrijs = o.InkoopPrijs,
                                                 Type_Id = o.Type_Id,
                                                 Website = o.Website,
                                                 Adressen = o.Adressen,
                                                 Thumb = oaf.Tumbnial
                                             }
                                 )
                    .Where(item => (item.Prijs >= minPrice || minPrice == 0) &&
                                   (item.Prijs <= maxPrice || maxPrice == 0) &&
                                   (bOffersOnly == false || item.Prijs < item.SPrijs) &&
                                   (item.Personen >= personen || personen == 0) &&
                                   (item.Slaapkamers == slaapkamers || slaapkamers == 0))
                    .Distinct();

To make more clear:

We need to get the following object:

public class FullObject
        {
            public int Object_Id { get; set; }
            public int? Adres_Id { get; set; }
            public int Contact_Id { get; set; }
            public string Naam { get; set; }
            public string Omschrijving { get; set; }
            public decimal? Prijs { get; set; }
            public decimal? SPrijs { get; set; }
            public int Personen { get; set; }
            public int Slaapkamers { get; set; }
            public string UrlKey { get; set; }
            public string Title { get; set; }
            public string Plaats { get; set; }
            public int ProviciesId { get; set; }
            public string Provicie { get; set; }
            public int? DepartmentId { get; set; }
            public string Department { get; set; }
            public bool Enabled { get; set; }
            public string GoogleMap_Url { get; set; }
            public decimal? InkoopPrijs { get; set; }
            public int Type_Id { get; set; }
            public string Website { get; set; }
            public List<Data.Object_Kenmerken> ItemKenmerken { get; set; }
            public Data.Adressen Adressen { get; set; }
            public string Thumb { get; set; }
        }

We have to filter on the following things

int[] iType
string search
int[] kenmerken
int iProvince
int iDepartement
decimal minPrice
decimal maxPrice
int personen
int slaapkamers
bool bOffersOnly

And this is (a little of) the table structure:

https://i.stack.imgur.com/3Y0Tf.jpg

We’ve tried to get everything separately but then we got a total of 12 seconds (and growing) because of the many objects that are in the database and we then need to get them all because of the filtering.

I hope someone thinks this is a challenge and see something I overlooked.

Thanks in advance.

Christiaan

Because this ticket is closed, my answer:

Thanks for all the replies.

The best was that one from @Daniel-Hilgarth. To not put the calculations in the query. To fixed that we’ve chosen to do the calculations in a SQL job that runs every 15 minutes and put the result directly in the Object table.

Next to that we have changed the joins a little bit to the database logic. Thanks to @Micheal-Samteladze.

All this changes give us the following query.

var result = (from o in db.Objects
                join omsc in db.Omschrijvingen on o.Omschrijving_Identifier equals omsc.Identifier
                where o.Enabled &&
                    (!iType.Any() || iType.Contains(o.Type_Id)) &&
                    (o.Prijs >= minPrice || minPrice == 0) &&
                    (o.Prijs <= maxPrice || maxPrice == 0) &&
                    (bOffersOnly == false || o.Prijs < o.SPrijs) &&
                    (o.Personen >= personen || personen == 0) &&
                    (o.Kamers == slaapkamers || slaapkamers == 0) &&
                    (iProvince < 1 || o.Adressen.Provincie_Id == iProvince) &&
                    (iDepartement < 1 || o.Adressen.Departement_Id == iDepartement) &&
                    o.Object_Logs.All(ol => ol.Log_Type != (int) ApplicationDefinitions.LogTypes.Deleted) &&
                    o.Object_Abonnement.Any(oa => oa.Betaald && oa.Tot >= DateTime.Now) &&
                    (!kenmerken.Any() || o.Object_Kenmerken.Count(ken => kenmerken.Contains(ken.Kenmerk_Id) && ken.Object_Id == o.Object_Id && ken.Waarde.ToUpper() != "FALSE" && ken.Waarde != "0") == kenmerken.Count()) &&
                    (search.Length < 1 || omsc.Naam.ToLower().Contains(search.ToLower()) || omsc.Omschrijving.ToLower().Contains(search.ToLower()) || o.Adressen.Woonplaats.ToLower().Contains(search.ToLower()))
                select new FullObject
                {
                    Object_Id = o.Object_Id,
                    Adres_Id = o.Adressen.Adres_Id,
                    DepartmentId = o.Adressen.Departement_Id,
                    Department = (o.Adressen.Departementen != null) ? o.Adressen.Departementen.Naam : "",
                    ProviciesId = o.Adressen.Provincie_Id,
                    Provicie = (o.Adressen.Provicies != null) ? o.Adressen.Provicies.Naam : "",
                    Contact_Id = o.Contact_Id,
                    Naam = omsc.Naam,
                    Omschrijving = omsc.Omschrijving,
                    Prijs = o.Prijs ?? 0,
                    SPrijs = o.SPrijs ?? 0,
                    Personen = o.Personen,
                    Slaapkamers = 0,
                    UrlKey = omsc.UrlKey,
                    Title = omsc.Title,
                    Plaats = o.Adressen.Woonplaats,
                    Enabled = o.Enabled,
                    GoogleMap_Url = o.GoogleMap_Url,
                    InkoopPrijs = o.InkoopPrijs,
                    Type_Id = o.Type_Id,
                    Website = o.Website,
                    Adressen = o.Adressen,
                    Thumb = o.Object_Afbeeldingen.FirstOrDefault(af => af.StandaardAfbeelding).Tumbnial
                }
            ) 
.Distinct();

This takes around 1 and a half second to give the first 10 result where the old one toke a minimum of 3.

But i’m still open for suggestions.

Thanks

Christiaan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:46:41+00:00Added an answer on June 14, 2026 at 3:46 pm

    When I’ve reviewed your query I could figure out that you are querying the database objects int your query.

    It will be much more faster if you push your logic in database and get the result in LINQ.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So for my programming class we have had a project to create a virtual
We have a project which had to be radically descoped in order to ship
I have a project hosted with git. I just recently had to reinstall ubuntu,
I have a project that's using git. When I received the repo, it had
We have a ASP.NET website project. In the past, we had been using asmx
Has anyone had this problem? My projects tend to have some long XML files
I want to apply NTFS-Search to our project. Our project have to find the
I have think this question few month for my .NET project (winform & webform)
Hey everyone, I had a couple of questions relating to the .NET Entity Framework
I'm working on a project with a large existing codebase and I've been tasked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.