1
\$\begingroup\$

I need to simplify this code as well as a Linq query:

 var data = from linq_row in trend_data.AsEnumerable()
                       group linq_row by linq_row["questionnaire_uniquename"] into g

                       select new
                       {
                           questionnaire_uniquename = g.First<DataRow>()["questionnaire_uniquename"].ToString(),
                           question_name = g.First<DataRow>()["question_name"].ToString(),
                           question_score_type = g.First<DataRow>()["question_score_type"].ToString(),
                           sparkline_data = g.Select(s => s["trend_score"].ToString()).ToArray()
                       }; ;
            int rowspan = 2;

Based on some condition, I am adding just adding filtering to an existing query and modifying the selected query here. Instead of selecting question_name, I want to select response_label from a data table.

if (criteria.CorporateSummaryExpand == "1")
{
    data =
       from linq_row in trend_data.AsEnumerable()
       group linq_row by linq_row["questionnaire_uniquename"] into g
       where g.First<DataRow>()["questionnaire_uniquename"].ToString() != "[Questionnaire].[Questionnaire].[Question].&[OFSP]&[OFSP5000]"
       && g.First<DataRow>()["questionnaire_uniquename"].ToString() != "[Questionnaire].[Questionnaire].[Question].&[OFSP]&[OFSP5008]"
       && g.First<DataRow>()["questionnaire_uniquename"].ToString() != "[Questionnaire].[Questionnaire].[Question].&[OFSP]&[OFSP1130]"

       select new
       {
           questionnaire_uniquename = g.First<DataRow>()["questionnaire_uniquename"].ToString(),
           question_name = g.First<DataRow>()["response_label"].ToString(),
           question_score_type = g.First<DataRow>()["question_score_type"].ToString(),
           sparkline_data = g.Select(s => s["trend_score"].ToString()).ToArray()
       };
    rowspan = 4;
}
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

From what you have provided you can combine the two queries into one. You should also consider removing constants like "1", 2, 4 with meaningful names.

var yourCondition = criteria.CorporateSummaryExpand == "1";
var data = from linq_row in trend_data.AsEnumerable()
           group linq_row by linq_row["questionnaire_uniquename"] into g
           where (criteria.CorporateSummaryExpand != "1") || (yourCondition  && g.First<DataRow>()["questionnaire_uniquename"].ToString() != "[Questionnaire].[Questionnaire].[Question].&[OFSP]&[OFSP5000]"
                   && g.First<DataRow>()["questionnaire_uniquename"].ToString() != "[Questionnaire].[Questionnaire].[Question].&[OFSP]&[OFSP5008]"
                   && g.First<DataRow>()["questionnaire_uniquename"].ToString() != "[Questionnaire].[Questionnaire].[Question].&[OFSP]&[OFSP1130]")
           select new
           {
                questionnaire_uniquename = g.First<DataRow>()["questionnaire_uniquename"].ToString(),
                question_name =yourCondition  ? g.First<DataRow>()["response_label"].ToString() : g.First<DataRow>()["question_name"].ToString(),
                question_score_type = g.First<DataRow>()["question_score_type"].ToString(),
                sparkline_data = g.Select(s => s["trend_score"].ToString()).ToArray()
           }; 
int rowspan = yourCondition ? 4 : 2;
\$\endgroup\$
6
  • \$\begingroup\$ thanks. but actually select statement also differed in condition. please check above question code \$\endgroup\$
    – SivaRajini
    Dec 11, 2014 at 13:28
  • \$\begingroup\$ In condition part ,question_name = g.First<DataRow>()["response_label"].ToString(), \$\endgroup\$
    – SivaRajini
    Dec 11, 2014 at 13:29
  • \$\begingroup\$ @SivaRajini I've fixed the code \$\endgroup\$ Dec 11, 2014 at 13:30
  • \$\begingroup\$ but repeating the condition again and again is that fine? \$\endgroup\$
    – SivaRajini
    Dec 11, 2014 at 13:32
  • \$\begingroup\$ because of that reason only i had placed in single if part \$\endgroup\$
    – SivaRajini
    Dec 11, 2014 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.