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

  • Home
  • SEARCH
  • 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 9312175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:33:47+00:00 2026-06-19T01:33:47+00:00

I have a piece of code that’s performing badly, and need to rewite it

  • 0

I have a piece of code that’s performing badly, and need to rewite it to introduce a proper where clause before starting the .ToList however, that’s where I’m getting stuck.

Currently the code looks lke this (roughly, I’ve taken some of the search criteria out to make it easier to display)

var Widgets = from b in _caspEntities.Widgets.Include("WidgetRegionLogs")
                    .Include("WidgetStatusLogs").Include("WidgetVoltageTests")
                        select b;

IEnumerable<Widget> results = Widgets.ToList();
if (comboBoxRegion.SelectedValue.ToString() != "0")
{
    results = from b in results
              where b.CurrentRegionLog != null && b.CurrentRegionLog.RegionId == int.Parse(comboBoxRegion.SelectedValue.ToString())
              select b;
}

if (comboBoxStatus.SelectedValue != null)
{
    results = from b in results
              where b.CurrentStatusLog != null && b.CurrentStatusLog.StatusId == comboBoxStatus.SelectedValue.ToString()
              select b;
}

if (txtCode.Text.Trim().Length > 0)
{
    results = from b in results
              where b.CodeNumber == txtCode.Text.Trim()
              select b;
}

dataGridViewWidget.DataSource = results.ToList();

I can write the SQL easily enough, essentially the model is simple, I have a Widget it has a RegionLog and a StatusLog, both of which store a history. The current region and status are retrieved from this by grouping by WidgetID and selecting the most recent Date Updated (and then going off to Region and Status tables to get the actual value).

So, I need to translate this into LINQ, but to be honest I don’t have a clue but am ken and willing to learn. In my head, I think I need to add some better where clauses, and then do the Widget.toList after I have applied the where clauses. I’m struggling with the CurrentRegionLog and CurrentStatusLog concepts as they are not populated until I run the IEnumerable.

If anyone can give some pointers, I’d be grateful,
Thanks

Edit – Added

    public BatteryRegionLog CurrentRegionLog
    {
        get { return _currentRegionLog; }
    }

    private BatteryRegionLog _currentRegionLog
    {
        get
        {
            if (this.BatteryRegionLogs.Count > 0)
            {
                BatteryRegionLog log = this.BatteryRegionLogs.OrderByDescending(item =>    item.LastModifiedDate).First();
                return log;
            }
            else
            {
                return null;
            }
        }
    }
  • 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-19T01:33:48+00:00Added an answer on June 19, 2026 at 1:33 am

    You can compose the query like this:

        if (comboBoxRegion.SelectedValue.ToString() != "0")
        {
            var id = int.Parse(comboBoxRegion.SelectedValue.ToString()
            Widgets = from b in Widgets
                      let currentRegionLog = 
                            b.BatteryRegionLogs
                             .OrderByDescending(item => item.LastModifiedDate)
                             .FirstOrDefault()
                      where currentRegionLog.RegionId == id)
                      select b;
        }
        ... // Same for the other criteria.
    
        dataGridViewWidget.DataSource = Widgets.ToList();
    

    The whole query is not executed before you do ToList(). As everything is translated to SQL you don’t need the null check b.CurrentRegionLog != null. SQL will evaluate b.CurrentRegionLog.RegionId == id just fine when there is no CurrentRegionLog.

    Edit

    Since CurrentRegionLog is a calculated property of your Widget class it cannot be translated into SQL. I made an effort to incorporate the code of calculated property into the query in a way that only the basic navigation property is used, so EF can translate it to SQL again.

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

Sidebar

Related Questions

I have a piece of code that was written by someone else before the
I have a piece of code that worked in 4.8 but now I need
I have this piece of code that searches for a folder given a starting
I have this piece of code that currently almost does what I need. It
I have a piece of code that involves multiple inserts but need to execute
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have a piece of code that load a very big image in memory.
I have a piece of code that takes several rows from a database and
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>

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.