Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
if (ProviderCountHealthConditions > 0)
                {
                    for (int i = 0; i < ProviderCountHealthConditions1; i++)
                    {

                        if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ12")
                        {
                            //1st inner row
                            if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString() == "ParalysisOrAmputation")
                            {

                                iTextSharp.text.pdf.PdfPTable tblHealthConditionsinner = new iTextSharp.text.pdf.PdfPTable(2);
                                tblHealthConditionsinner.TotalWidth = 550f;
                                tblHealthConditionsinner.AddCell(GetCellImage(chkdImg));
                                tblHealthConditionsinner.AddCell(GetInnerCell("ParalysisOrAmputation", "", 2, 1));

                                tblHealthConditions.AddCell(tblHealthConditionsinner);


                            }
                            for (i = 0; i < ProviderCountHealthConditions1; i++)
                            {
                                if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ24")
                                {
                                    iTextSharp.text.pdf.PdfPTable tblinner = new iTextSharp.text.pdf.PdfPTable(4);
                                    tblinner.TotalWidth = 10f;
                                    tblinner.AddCell(GetCellImage(chkdImg));
                                    tblinner.AddCell(GetInnerCell("Paralysis", "", 1, 1));
                                    for (i = 0; i < ProviderCountHealthConditions1; i++)
                                    {
                                        if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ18")
                                        {
                                            if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString() == "ParalysisOrAmputation")
                                            {
                                                tblinner.AddCell(new iTextSharp.text.Phrase("DESCRIBE: " + iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorText.ToString(), fText));



                                            }

                                        }
                                        if (iPatientInfoHealthConditions.ElementAtOrDefault(i).QCode.ToString() == "HealthConditionsQ19")
                                        {
                                            if (iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorDesc.ToString() == "ParalysisOrAmputation")
                                            {

                                                tblinner.AddCell(new iTextSharp.text.Phrase("Mode of Mobility:" + iPatientInfoHealthConditions.ElementAtOrDefault(i).FactorText.ToString(), fText));


                                            }

                                        }
                                    }
                                    tblHealthConditions.AddCell(tblinner);

                                }
                            }

                        }
Posted
Updated 26-Oct-15 21:10pm
v3
Comments
Maciej Los 27-Oct-15 3:04am    
We aren't diviners. We do not see your screen and haven't access to your data. Please be more specific and provide more details about your issue.
Naveen.Sanagasetti 27-Oct-15 3:58am    
You know the problem right? because of looping iterations you are getting delay of execution then check whether there is a chance to skip that iteration. If there is a chance then do that, how do you expect help from our end regarding this. We are helpless in this situation this is totally your project setup, you need to discuss with your lead and come to know one solution for the same.

You're looping through all items in iPatientInfoHealthConditions just to find the 1 item you're after. You then often throw that item away after looking for it.

I have no idea how large the list, but that's not required. You can just get the item you need using linq:

Use IEnumerable.Any([condition]) to check if an item exists.
Use IEnumerable.SingleOrDefault([condition]) to find a specific item, then check that it is not null (null means it was not found)

C#
if (ProviderCountHealthConditions > 0)
{
    if (iPatientInfoHealthConditions.Any(pihc => pihc.QCode.ToString() == "HealthConditionsQ12" && pihc.FactorDesc.ToString() == "ParalysisOrAmputation"))
    {
        iTextSharp.text.pdf.PdfPTable tblHealthConditionsinner = new iTextSharp.text.pdf.PdfPTable(2);
        tblHealthConditionsinner.TotalWidth = 550f;
        tblHealthConditionsinner.AddCell(GetCellImage(chkdImg));
        tblHealthConditionsinner.AddCell(GetInnerCell("ParalysisOrAmputation", "", 2, 1));

        tblHealthConditions.AddCell(tblHealthConditionsinner);
    }

    if (iPatientInfoHealthConditions.Any(pihc => pihc.QCode.ToString() == "HealthConditionsQ24"))
    {
        iTextSharp.text.pdf.PdfPTable tblinner = new iTextSharp.text.pdf.PdfPTable(4);
        tblinner.TotalWidth = 10f;
        tblinner.AddCell(GetCellImage(chkdImg));
        tblinner.AddCell(GetInnerCell("Paralysis", "", 1, 1));
                    
            var healthConditionsQ18 =
                iPatientInfoHealthConditions.SingleOrDefault(pihc => pihc.QCode.ToString() == "HealthConditionsQ18" && pihc.FactorDesc.ToString() == "ParalysisOrAmputation");
            if (healthConditionsQ18 != null)
            {
                tblinner.AddCell(new iTextSharp.text.Phrase("DESCRIBE: " + healthConditionsQ18.FactorText.ToString(), fText));

            }
            var healthConditionsQ19 =
                iPatientInfoHealthConditions.SingleOrDefault(pihc => pihc.QCode.ToString() == "HealthConditionsQ19" && pihc.FactorDesc.ToString() == "ParalysisOrAmputation");

            if (healthConditionsQ19!=null)
            {
                    tblinner.AddCell(
                        new iTextSharp.text.Phrase("Mode of Mobility:" + healthConditionsQ19.FactorText.ToString(), fText));
                            
            }
                    
        tblHealthConditions.AddCell(tblinner);
    }
}
 
Share this answer
 
Comments
lukeer 28-Oct-15 4:15am    
Are you sure that the linq lookup doesn't loop in the background? Does it use some clever hashing wihtout the user preparing it?
Andy Lanng 28-Oct-15 4:54am    
It entirely depends on the source enumerable. It will always try to use the most efficient method, which is a great shortcut to trying to figure out that method yourself in each case. Either way, it is hella more readable :)
1) Skip unnecessary iterations.
As Naveen.Sanagasetti said in a comment, skip as large portions of lists as possible if you are able to tell beforehand that this portion won't fit your if condition.

2) Don't ToString() to compare.
Your if conditions compare the result of ToString() with string literals. If possible, compare QCode and FactorDesc directly, without calling ToString(). Since there's no information about their types, I can't go into details on that. But if QCode is an integer (of whatever size) or enumeration, a comparison will be significantly faster than first converting it.

3) Suspend UI updates
Since you didn't specify a UI technology, I'll assume Windows Forms. Chances are that AddCell() will refresh its table every time it is called. If that's often, it will get surprisingly time-consuming.
You can suspend UI layout, perform all your loops, checks and additions and then resume UI layout to get all UI changes to the screen in one go.
That's faster because each one of the small changes has to rework the complete UI around (or at least to the right or bottom of) it, whilst one big change has to do that only once.
 
Share this answer
 
Comments
sukumar kathula 30-Oct-15 7:32am    
Thanq it's working fine
Here are some general ideas in a code example (that will compile, but is not a working solution).

The ideas I hope you will get from this example are:

1. eliminate possible errors before you start loops

2. use switch/case statements for improved speed, and for greater code maintainability over time. If you use an integer switch criterion, particularly if the integers are in a linear sequence, i.e., 0,1,2 ... etc. ... this will be compiled as a jump-table, and will give maximum performance.

Assuming a "HealthCondition" (a guess !) is something like:
C#
public class ProviderHealthCondition
{
    public int Index { set; get; }
    public string ConditionName { set; get; }
    public List<string> Factors { set; get; }

    public ProviderHealthCondition(int index, string condition, params string[] factors)
    {
        Index = index;
        ConditionName = condition;
        Factors = factors.ToList<string>();
    }
}

public static class TreatmentMap
{
    public static void MapConditionsToTreatment(List<ProviderHealthCondition> conditions)
    {
        if (conditions.Count == 0) throw new ArgumentException("error: 0 ProviderCountHealthConditions");

        ProviderHealthCondition currentCondition;
        string currentQCode;
        string currentFactor;
        
        for (int i = 0; i < conditions.Count; i++)
        {
            currentCondition = conditions[i];

            currentQCode = currentCondition.ConditionName;

            // not working ... since I can't see how you select which Factor
            // to use in your code
            currentFactor = currentCondition.Factors[i];

            switch (currentQCode)
            {
                case "HealthConditionsQ12":
                    // switch on the current Factor
                    switch (currentFactor)
                    {
                        case "ParalysisOrAmputation":

                            break;

                        case "CantWalkAndChewGum":

                            break;
                    }
                   break;

                case "HealthConditionsQ24":

                    break;

                // and so on ...

                default:
                    break;
            }
        }
    }
}


[Edit: MTH: Just a couple of markup errors. No logic changes.]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900