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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:53:41+00:00 2026-06-02T22:53:41+00:00

Suppose I have a class representing order lines , eg public class Line {

  • 0

Suppose I have a class representing order lines , eg

public class Line
{
    public string Code ;
    public string No ; // Invoice Number
    public DateTime Date ;
    public string Product ;
    public decimal Quantity ;
}

and a List of lines, eg

List<Line> myList = new List<Line>();
myList.Add(new Line() { Code = "ABC001", No = "1001" ,Date = new DateTime(2012,4,1) ,  Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC001", No = "1001" ,Date = new DateTime(2012,4,1) ,  Product = "Y", Quantity= 1m});

myList.Add(new Line() { Code = "ABC002", No = "1002" ,Date = new DateTime(2012,4,2) ,  Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC002", No = "1002" ,Date = new DateTime(2012,4,2) ,  Product = "Y", Quantity= 1m});
myList.Add(new Line() { Code = "ABC002", No = "1003" ,Date = new DateTime(2012,4,3) ,  Product = "Z", Quantity= 1m});
myList.Add(new Line() { Code = "ABC002", No = "1004" ,Date = new DateTime(2012,4,4) ,  Product = "X", Quantity= 1m});

myList.Add(new Line() { Code = "ABC003", No = "1005" ,Date = new DateTime(2012,4,4) ,  Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC003", No = "1006" ,Date = new DateTime(2012,4,4) ,  Product = "X", Quantity= 1m});
myList.Add(new Line() { Code = "ABC003", No = "1006" ,Date = new DateTime(2012,4,4) ,  Product = "Y", Quantity= 1m});

I am looking to retrieve all lines where the Customer Code has more than one invoice.
To do this, I am first of all grouping by the Code, No and Date and then grouping that by customer Code and for any customers which two or more records, I am selecting all but the first record.

Like so:

var query1 = 
    (from r in myList
        group r by new { r.Code ,  r.No , r.Date } into results
        group results by new { results.Key.Code } into results2 
        where results2.Count() > 1
        select new 
        {   
            results2.Key.Code , 
            Count = results2.Count(), 
            Results = results2.OrderBy(i=>i.Key.Date).Skip(1).ToList() 
          // Skip the first invoice
        } 
     ).ToList();

query1 now contains the correct records, but wrapped inside IGrouping and I am having trouble gets the results out as a List<Line>

I tried
query1.SelectMany(r=>r.Results).ToList();

but this still leaves me with IGrouping and that’s where I am stuck.

I could resort to the nested for loops as in

List<Line> output = new List<Line>();
foreach (var r1 in query1)
{
    foreach(var r2 in r1.Results)
        foreach(var r3 in r2)
            output.Add(r3);     
}

but is there a better/Linq way?

The actual output should be four lines as in

// Code    No    Date              Product Quantity 
// ABC002 1003 03/04/2012 00:00:00 Z 1 
// ABC002 1004 04/04/2012 00:00:00 X 1 
// ABC003 1006 04/04/2012 00:00:00 X 1 
// ABC003 1006 04/04/2012 00:00:00 Y 1 
  • 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-06-02T22:53:47+00:00Added an answer on June 2, 2026 at 10:53 pm

    You’re going to kick yourself:

    query1.SelectMany(q => q);
    
    ABC002 1003 3/04/2012 12:00:00 AM Z 1 
    ABC002 1004 4/04/2012 12:00:00 AM X 1 
    ABC003 1006 4/04/2012 12:00:00 AM X 1 
    ABC003 1006 4/04/2012 12:00:00 AM Y 1 
    

    The return from query1 is an enumerable (I removed your lists) of IGrouping and IGrouping is itself an enumerable, so we can just flatten it directly.

    See here: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/09/28/9836.aspx

    Edit: Remembered I also simplified your code:

    var query1 = 
    (from r in myList
        group r by new { r.Code ,  r.No , r.Date } into results
        group results by new { results.Key.Code } into results2 
        where results2.Count() > 1
        from result in results2.OrderBy(i=>i.Key.Date).Skip(1)
        select result
     );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have: class Foo { public String Bar { get; set; } }
Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose I have: class Foo { ... }; class Bar : public Foo {
Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have a class with a string instance attribute. Should I initialize this
Suppose you have a class NonCopyable class NonCopyable { public: NonCopyable(int n){} ~NonCopyable(){} [...]
Suppose I have a simple interface representing a complex number, whose instances would be

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.