Identical linq for different index I am parsing excel data and need to create object based on StartDate-EndDate difference.

Is there a better way to have only one linq perform this task and return two different result sets ? Only difference in both linq is just the variable on which its applied.

private Dictionary&lt;string, Dictionary&lt;DateTime, List&lt;DataPoint&gt;&gt;&gt;[] ParseData(ExcelFile file, Dictionary&lt;string, string&gt;[] ConfigMap)
{

    // Extract the information from the excel file
    var excelData = file.GetWorksheetData(sheet: 0, rowStart: 2);

    var monthlyRows = excelData.Rows.Where(r =&gt; (DateTime.FromOADate((double)r[EndDateCol]) - DateTime.FromOADate((double)r[StartDateCol])).Days &gt; 1);
    var dailyRows = excelData.Rows.Where(r =&gt; (DateTime.FromOADate((double)r[EndDateCol]) - DateTime.FromOADate((double)r[StartDateCol])).Days == 1);

    var monthlyData = monthlyRows.Where(r =&gt; r[KeyCol] != null &amp;&amp; ConfigMap[0].ContainsKey((string)r[KeyCol]))
                                   .GroupBy(r =&gt; (string)r[KeyCol])
                                   .ToDictionary(g =&gt; g.Key,
                                                 g =&gt; g.GroupBy(r =&gt; DateTime.FromOADate((double)r[DateCol]))
                                                       .ToDictionary(c =&gt; c.Key,
                                                                     c =&gt; c.Select(r =&gt; new DataPoint(DateTime.FromOADate((double)r[StartDateCol]), new Decimal((double)r[PriceCol])))
                                                                           .ToList()));

    var dailyData = dailyRows.Where(r =&gt; r[KeyCol] != null &amp;&amp; ConfigMap[1].ContainsKey((string)r[KeyCol]))
                                  .GroupBy(r =&gt; (string)r[KeyCol])
                                  .ToDictionary(g =&gt; g.Key,
                                                g =&gt; g.GroupBy(r =&gt; DateTime.FromOADate((double)r[DateCol]))
                                                      .ToDictionary(c =&gt; c.Key,
                                                                    c =&gt; c.Select(r =&gt; new DataPoint(DateTime.FromOADate((double)r[StartDateCol]), new Decimal((double)r[PriceCol])))
                                                                          .ToList()));

    return new Dictionary&lt;string, Dictionary&lt;DateTime, List&lt;DataPoint&gt;&gt;&gt;[] { monthlyData, dailyData };
}
Assuming monthlyRows and dailyRows are both IEnumerable&lt;ExcelDataRow&gt;, a first step could be to extract a method (assuming return type here, I only skimmed through your code):

private Dictionary&lt;DateTime, List&lt;DataPoint&gt;&gt; GetDataDictionary(IEnumerable&lt;ExcelDataRow&gt; data)
{
    var result = data.Where(r =&gt; r[KeyCol] != null &amp;&amp; ConfigMap[0].ContainsKey((string)r[KeyCol]))
                                   .GroupBy(r =&gt; (string)r[KeyCol])
                                   .ToDictionary(g =&gt; g.Key,
                                                 g =&gt; g.GroupBy(r =&gt; DateTime.FromOADate((double)r[DateCol]))
                                                       .ToDictionary(c =&gt; c.Key,
                                                                     c =&gt; c.Select(r =&gt; new DataPoint(DateTime.FromOADate((double)r[StartDateCol]), new Decimal((double)r[PriceCol])))
                                                                           .ToList()));    
    return result;
}


Your ParseData method's body could then look like this:

    // Extract the information from the excel file
    var excelData = file.GetWorksheetData(sheet: 0, rowStart: 2);

    var monthlyRows = excelData.Rows.Where(r =&gt; (DateTime.FromOADate((double)r[EndDateCol]) - DateTime.FromOADate((double)r[StartDateCol])).Days &gt; 1);
    var monthlyData = GetDataDictionary(monthlyRows);

    var dailyRows = excelData.Rows.Where(r =&gt; (DateTime.FromOADate((double)r[EndDateCol]) - DateTime.FromOADate((double)r[StartDateCol])).Days == 1);
    var dailyData = GetDataDictionary(dailyRows);

    return new Dictionary&lt;string, Dictionary&lt;DateTime, List&lt;DataPoint&gt;&gt;&gt;[] { monthlyData, dailyData };




That solves one issue.

Next step would be to break down the LINQ query and make the code self-explanatory as far as why you need to have a dictionary of dictionaries of lists.. and then make that simpler.