Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7442207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:08:26+00:00 2026-05-29T11:08:26+00:00

Hi I have the following code which returns me the right data but it

  • 0

Hi I have the following code which returns me the right data but it seems there must be a better way to combine 3 lists, based on their common field(s) and transpose the results out into a new list of a given type using LINQ, instead of resorting to the foreach at the end. Any ideas?

  public IEnumerable<StagSummaryByCflHistoricalItem> GetSummaryByCflHistorical(DateTime currentDate)
  {
     var allRecords =
        this.preGrantSummaryHistoricalRepository
           .AllWithFetch(this.preGrantSummaryHistoricalRepository.All, x => x.CaseFileLocation)
           .Where(
           x => x.Date >= currentDate.FirstDayOfQuarterFromDateTime()
           && x.Date <= currentDate.LastDayOfQuarterFromDateTime())
           .ToList();

     var summaryForQuarter =
        allRecords.GroupBy(x => new { x.CaseFileLocation.Id, x.CaseFileLocation.Name }).Select(
           x =>
           new
              {
                 CaseFileLocationId = x.Key.Id,
                 Description = x.Key.Name,
                 TotalCasesEnteredCfl = x.Sum(y => y.TotalCasesEntered),
                 TotalNetFeeEnteredCfl = x.Sum(y => y.TotalNetFeeEntered),
                 TotalCasesLeftCfl = x.Sum(y => y.TotalCasesLeft),
                 TotalNetFeeLeftCfl = x.Sum(y => y.TotalNetFeeLeft)
              })
              .OrderBy(x => x.CaseFileLocationId)
              .ToList();

     var summaryForMonth =
        allRecords.Where(x => x.Date >= currentDate.FirstDayOfMonthFromDateTime())
        .GroupBy(x => new { x.CaseFileLocation.Id, x.CaseFileLocation.Name }).Select(
           x =>
           new
              {
                 CaseFileLocationId = x.Key.Id,
                 Description = x.Key.Name,
                 TotalCasesEnteredCfl = x.Sum(y => y.TotalCasesEntered),
                 TotalNetFeeEnteredCfl = x.Sum(y => y.TotalNetFeeEntered),
                 TotalCasesLeftCfl = x.Sum(y => y.TotalCasesLeft),
                 TotalNetFeeLeftCfl = x.Sum(y => y.TotalNetFeeLeft)
              })
              .OrderBy(x => x.CaseFileLocationId)
              .ToList();

     var summaryForWeek =
        allRecords.Where(x => x.Date >= currentDate.FirstDayOfWeekFromDateTime(DayOfWeek.Monday)).GroupBy(
           x => new { x.CaseFileLocation.Id, x.CaseFileLocation.Name }).Select(
              x =>
              new
                 {
                    CaseFileLocationId = x.Key.Id,
                    Description = x.Key.Name,
                    TotalCasesEnteredCfl = x.Sum(y => y.TotalCasesEntered),
                    TotalNetFeeEnteredCfl = x.Sum(y => y.TotalNetFeeEntered),
                    TotalCasesLeftCfl = x.Sum(y => y.TotalCasesLeft),
                    TotalNetFeeLeftCfl = x.Sum(y => y.TotalNetFeeLeft)
                 })
                 .OrderBy(x => x.CaseFileLocationId)
                 .ToList();

     var finalList = summaryForQuarter
        .Select(x => new StagSummaryByCflHistoricalItem()
           {
              CaseFileLocationId = x.CaseFileLocationId,
              Description = x.Description,
              QuarterTotalCasesEnteredCfl = x.TotalCasesEnteredCfl,
              QuarterTotalCasesLeftCfl = x.TotalCasesLeftCfl,
              QuarterTotalNetFeeEnteredCfl = x.TotalNetFeeEnteredCfl,
              QuarterTotalNetFeeLeftCfl = x.TotalNetFeeLeftCfl
           })
           .OrderBy(x => x.CaseFileLocationId)
           .ToList();

     foreach (var qrt in finalList)
     {

        var mnthData = summaryForMonth.FirstOrDefault(x => x.CaseFileLocationId == qrt.CaseFileLocationId);

        if (mnthData != null)
        {
           qrt.MonthTotalCasesEnteredCfl = mnthData.TotalCasesEnteredCfl;
           qrt.MonthTotalCasesLeftCfl = mnthData.TotalCasesLeftCfl;
           qrt.MonthTotalNetFeeEnteredCfl = mnthData.TotalNetFeeEnteredCfl;
           qrt.MonthTotalNetFeeLeftCfl = mnthData.TotalNetFeeLeftCfl;
        }

        var weekData = summaryForWeek.FirstOrDefault(x => x.CaseFileLocationId == qrt.CaseFileLocationId);
        if (weekData == null)
        {
           continue;
        }
        qrt.WeekTotalCasesEnteredCfl = weekData.TotalCasesEnteredCfl;
        qrt.WeekTotalCasesLeftCfl = weekData.TotalCasesLeftCfl;
        qrt.WeekTotalNetFeeEnteredCfl = weekData.TotalNetFeeEnteredCfl;
        qrt.WeekTotalNetFeeLeftCfl = weekData.TotalNetFeeLeftCfl;
     }

     return finalList;
  }

Note: I am intentionally getting the entire quarter’s worth of data first as a list and then operating on it to do the month & quarter totals but this is mainly because I cannot fathom a way to get the end result from a combined LINQ IQuerable.

I am using NHibernate, LINQ method syntax, the repository pattern and SQL Server 2008

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:08:26+00:00Added an answer on May 29, 2026 at 11:08 am

    If I’m reading your code correctly, you’re projecting your results to anonymous types with the same resulting members, which makes it very easy to join your results together. I did something similar recently with Union. Here’s a simplified example I just wrote to demonstrate:

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    
    namespace ConsoleApplication1 {
        public class Month {
            public int MonthID { get; set; }
            public string MonthName { get; set; }
            public int NoDays { get; set; }
        }
    
        internal class Program {
            private static void Main(string[] args) {
                // Build up months
                var months = new List<Month>();
                for (var i = 1; i <= 12; i++) {
                    months.Add(new Month {
                        MonthID = i,
                        MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(i),
                        NoDays = DateTime.DaysInMonth(2012, i)
                    });
                }
    
                var w = months.Select(m => new {
                    m.MonthName
                });
    
                var x = months.Select(m => new {
                    m.MonthName
                });
    
                var y = months.Select(m => new {
                    m.MonthName
                });
    
                var z = w.Union(x).Union(y);
    
                foreach (var m in z) {
                    Console.WriteLine(m.MonthName);
                }
                Console.Read();
            }
        }
    }
    

    Bear in mind that “Union” (like the SQL UNION clause) will remove any duplicates from your list. If you don’t want to remove duplicates (i.e. perform a “union all”), use “Concat” as follows:

    var z = w.Concat(x).Concat(y);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposing I have the following code which returns a Javascript object which I can
I have the following code which works fine. However, I only want to return
I have some JS code which generates the following object, return { type: some
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which should put programs startable in Bash. if [
I have the following code which reads in the follow file, append a \r\n
I have the following code which re-uses a CookieContainer which logs in on the
I have the following code which I stripped out of any non-essential lines to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.