Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need to simplify the below code as well 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 filtering to an existing query and modifying the select query here. Instead of selecting "question_name", I want to select "response_label" from 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;
            }
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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;
share|improve this answer
    
thanks. but actually select statement also differed in condition. please check above question code –  SivaRajini Dec 11 '14 at 13:28
    
In condition part ,question_name = g.First<DataRow>()["response_label"].ToString(), –  SivaRajini Dec 11 '14 at 13:29
    
@SivaRajini I've fixed the code –  Vsevolod Goloviznin Dec 11 '14 at 13:30
    
but repeating the condition again and again is that fine? –  SivaRajini Dec 11 '14 at 13:32
    
because of that reason only i had placed in single if part –  SivaRajini Dec 11 '14 at 13:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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