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 6215093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:59:35+00:00 2026-05-24T06:59:35+00:00

The problem below is in the code, i.e. the bit that says DY1 =,

  • 0

The problem below is in the code, i.e. the bit that says DY1 =, DY2 =, etc. I don’t know how many there will be so I need to do it dynamically.

Here is my textfile :

Product, Origin Year, Development Year, Incremental Value
Comp, 1992, 1992, 110.0
Comp, 1992, 1993, 170.0
Comp, 1993, 1993, 200.0
Non-Comp, 1990, 1990, 45.2
Non-Comp, 1990, 1991, 64.8
Non-Comp, 1990, 1993, 37.0
Non-Comp, 1991, 1991, 50.0
Non-Comp, 1991, 1992, 75.0
Non-Comp, 1991, 1993, 25.0
Non-Comp, 1992, 1992, 55.0
Non-Comp, 1992, 1993, 85.0
Non-Comp, 1993, 1993, 100.0

Here is my code (pasted from LinqPad) :

void Main()
{
  using (var reader = new StreamReader("C:\\temp\\ExampleData.txt"))
  {
    var products =
        (from line in reader.Lines()
        where !line.StartsWith("Product")
        let parts = line.Split(',')
        select new {Product=parts[0], Origin_Year=parts[1], Development_Year=parts[2], Incremental_Value=parts[3], DY=int.Parse(parts[2]) - int.Parse(parts[1]) + 1 }).ToList();
    products.Dump();

    var grps = from p in products
    group p by new {p.Product, p.Origin_Year}
    into g
    select (new { g.Key.Product, g.Key.Origin_Year, 

    DY1 = g.Where(c => c.DY == 1).Select (c => c.Incremental_Value).FirstOrDefault(),
    DY2 = g.Where(c => c.DY == 2).Select (c => c.Incremental_Value).FirstOrDefault(),
    DY3 = g.Where(c => c.DY == 3).Select (c => c.Incremental_Value).FirstOrDefault(),
    DY4 = g.Where(c => c.DY == 4).Select (c => c.Incremental_Value).FirstOrDefault()    
    });

    grps.Dump();
  }
}

}// Temporary hack to enable extension methods

/// <summary>
/// Operators for LINQ to Text Files
/// </summary>
public static class Extensions
{
  public static IEnumerable<String> Lines(this TextReader source)
  {
    String line;

    if (source == null)
      throw new ArgumentNullException("source");

    while ((line = source.ReadLine()) != null)
      yield return line;
  }

Here is my output :

Product   Origin_Year  DY1    DY2    DY3   DY4 
Comp      1992         110.0  170.0  null  null  
Comp      1993         200.0  null   null  null  
Non-Comp  1990         45.2   64.8   null  37.0  
Non-Comp  1991         50.0   75.0   25.0  null  
Non-Comp  1992         55.0   85.0   null  null  
Non-Comp  1993         100.0  null   null  null 
  • 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-24T06:59:36+00:00Added an answer on May 24, 2026 at 6:59 am

    The best suggestion I can offer is to return an array of values for each possible DY.

    Start by modifying Incremental_Value field in the products query to be a double (ie double.Parse(parts[3])).

    Now create an enumerable for the maximum number of DYs:

        var dys = Enumerable.Range(1, products.Max(p => p.DY));
    

    Then the grps query looks like this:

        var grps =
            from p in products
            group p by new
            {
                p.Product,
                p.Origin_Year,
            } into g
            let lookup = g.ToLookup(x => x.DY, x => x.Incremental_Value)
            select new
            {
                g.Key.Product,
                g.Key.Origin_Year, 
                DYs = dys.Select(x => lookup[x].Sum()).ToArray(),
            };
    

    Does that work for you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to return a unsigned int* from a function. The code below will
Below is a very simple bit of code that mimics a class structure in
The view code below is a bit verbose, so let me state my problem
I have some problem with my cfml website. I have used the below code
The code below is (hopefully) a minimized testcase of my original problem. I'm trying
The code below is working, but with a problem I do not understand. When
I am having a very specific problem in JQuery The code below is used
Having a problem getting a TreeView control to display node images. The code below
I am having a problem using the signedCMS.decode routine. See the code below. The
Note: Sorry for the amount of pseudo code below, but I didn't know how

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.