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 7896469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:54:10+00:00 2026-06-03T07:54:10+00:00

I use this method called SearchConsequences to iterate through List<ValuesEO> of objects and perform

  • 0

I use this method called SearchConsequences to iterate through List<ValuesEO> of objects and perform some tasks, for getting values of particular fields, according to applied rules. I want to somehow simplify this code.

I want to switch (replace) everywhere in code the expression ValuesEO[i].powerR for other ValuesEO[i].otherField in the whole block of code.

At this time I do this just by block coping and changing it manually. So lets say, at the end, I have 5 blocks of really similar code blocks in this method. The only difference is in ValuesEO[i].otherField than ValuesEO[i].otherField2 ValuesEO[i].otherField3 … and so on.

I don’t like that block coping.

public Dictionary<Consequence,Cause> SearchConsequences(List<ResultsCatcher> smallTable, int n, ConnectHYSYS obj, int keyP, int keyR)//for one stream for one parameter
    {
        double threshold = 0.005;

        Dictionary<Consequence,Cause> collection = new Dictionary<Consequence,Cause>();

        //search in ValesE for each energy stream, for powerR
        for (int i = 0; i < smallTable[n].ValuesE.Count; i++)
        {
            //sort the smallTable
            smallTable.Sort((x, y) => x.ValuesE[i].powerR.CompareTo(y.ValuesE[i].powerR));

            //get the index of first occurrence of powerR >= threshold, if there is nothing bigger than threshold, index is null
            var tagged = smallTable.Select((item, ii) => new { Item = item, Index = (int?)ii });
            int? index = (from pair in tagged
                          where pair.Item.ValuesE[i].powerR >= threshold
                          select pair.Index).FirstOrDefault();

            //get needed information
            if (index != null)
            {
                int id = Convert.ToInt16(index);

                double newValue = smallTable[id].ValuesE[i].power;
                double newValueR = smallTable[id].ValuesE[i].powerR;
                TypeOfValue kindOf = TypeOfValue.power;
                Consequence oneConsequence = new Consequence(obj.EnergyStreamsList[i], newValue, newValueR, kindOf);

                Cause oneCause = new Cause();
                oneCause.GetTableHeader(smallTable[id]);

                collection.Add(oneConsequence,oneCause);
            }
        }
    }

Maybe it is easy to accomplish that and somewhere this problem is discussed.
But I really even don’t know how to google it.

  • 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-03T07:54:11+00:00Added an answer on June 3, 2026 at 7:54 am

    This is a ready made program for you that demonstrates how to move your criteria/property selection outside your examination function. Have a look to see how fields criterias are matched.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management;
    
    namespace Test
    {
        class Program
        {
            public class PowerValues
            {
                public double power;
                public double powerR;
                public double lightbulbs;
                public double lightbulbsR;
            }
    
            public static void DoSomething(IEnumerable<PowerValues> powerValues, Func<PowerValues, double> criteria, double treshhold)
            {
                var flaggedElements = powerValues.Where(e => criteria(e) > treshhold);
                foreach (var flagged in flaggedElements)
                {
                    Console.WriteLine("Value flagged: {0}", criteria(flagged));
                }
            }
    
            public static void Main(string[] args)
            {
                List<PowerValues> powerValues = new List<PowerValues>();
    
                powerValues.Add(new PowerValues(){power=10, powerR=0.002, lightbulbs = 2, lightbulbsR = 2.006});
                powerValues.Add(new PowerValues(){power=5, powerR=0.004, lightbulbs = 4, lightbulbsR = 2.09});
                powerValues.Add(new PowerValues(){power=6, powerR=0.003, lightbulbs = 3, lightbulbsR = 2.016});
    
                Console.WriteLine("Power matching criteria . . . ");
                DoSomething(powerValues, (e) => e.powerR, 0.003);
                Console.WriteLine("Lightbulbs matching criteria . . . ");
                DoSomething(powerValues, (e) => e.lightbulbs, 3);
    
                Console.Write("Press any key to continue . . . ");
                Console.ReadKey(true);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use GridBagLayout. I have the method called buildLabel. This creates three label.
Suppose that I have a method called doSomething() and I want to use this
I have a method called GetAge(DateTime birthDay). I want to use this method in
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
actually i use this method to show similar words for a search request.. $query
I'd like to use this method to create user-friendly URL. Because my site is
When i use this method, its throws illegal state expression on blackberry simulator. protected
When I use the this method to initialize a UITabBarItem : - (id)initWithTitle:(NSString *)title
We use this small utility method. But we don't like it. Since it's not
I have this method call I have to use... financial_document.assets.length But financial_document.assets could be

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.