Search This Blog

Wednesday, January 15, 2014

LINQ Example instead of If Condition

Today, as part of code review. we found small variants on is condition and LINQ. we are used if condition. Instead of that we can write same thing in LINQ, it is more flexible in terms of readability and performance. Other alternative is Switch case.

if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "SO")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "SON");
            }

            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "SP")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "SPOUSE");
            }

            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "GS")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "GRAND SON");
            }

            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "FA")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "FATHER");
            }

            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "DA")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "DAUGHTER");
            }

            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "SP")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "SPOUSE");
            }

            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "GD")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "GRAND DAUGHTER");
            }

            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()) && dr["PRO_REL_WITH_ANNUITANT"].ToString() == "MO")
            {
                pdfFormFields.SetField("Relationship_with_Annuitant", "MOTHER");
            }
 **********
Dictionary<string, string> Annutant_Relation = new Dictionary<string, string>();
            Annutant_Relation.Add("SO", "SON");
            Annutant_Relation.Add("SP", "SPOUSE");
            Annutant_Relation.Add("GS", "GRAND SON");
            Annutant_Relation.Add("FA", "FATHER");
            Annutant_Relation.Add("DA", "DAUGHTER");
            Annutant_Relation.Add("GD", "GRAND DAUGHTER");
            Annutant_Relation.Add("MO", "MOTHER");           
            if (!string.IsNullOrEmpty(dr["PRO_REL_WITH_ANNUITANT"].ToString()))
            {                pdfFormFields.SetField("Relationship_with_Annuitant", Annutant_Relation.Where(f => f.Key == dr["PRO_REL_WITH_ANNUITANT"].ToString()).Select(n => n.Value).FirstOrDefault().ToString().Trim());
            }


No comments:

Post a Comment