7

I want to achieve something similar to this. But I don't know in what manner I can use that solution.

My entity has these properties

CustomerName
Date
SortOrder

I've whole list of this Entity. What I want to do is, group all those items in List<> which have consecutive SortOrder and same Date and same CustomerName

Example input

  var inv = new List<Invoice>(){
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 0},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 1},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 2},
    new Invoice(){ CustomerName = "xyz" ,Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 3},
    new Invoice(){ CustomerName = "xyz" ,Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 4},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 5},
    new Invoice(){ CustomerName = "Abc" ,Date = DateTime.Today, SortOrder = 6}
  };

Example output

  var invGrouped = new List<List<Invoice>>
   {
     new List<Invoice>
       {
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 0},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 1},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 2}
       },
     new List<Invoice>
       {
         new Invoice {CustomerName = "xyz", Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 3},
         new Invoice {CustomerName = "xyz", Date = DateTime.Today.Subtract(TimeSpan.FromDays(1)), SortOrder = 4}
       },
     new List<Invoice>
       {
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 5},
         new Invoice {CustomerName = "Abc", Date = DateTime.Today, SortOrder = 6}

       }
   };

UPDATE
Non-LINQ solution will also suffice.

Community
  • 1
  • 1
IsmailS
  • 10,797
  • 21
  • 82
  • 134
  • 2
    It would greatly help if you could include a small sample of data as well as exactly what you expect that data to look like once it has been grouped. – diceguyd30 Oct 26 '10 at 12:17
  • Why aren't the two bottom rows (with CustomerName "Abc" and DateTime of Today) in the same list as the other Customer "Abc" orders placed today? – Doctor Jones Oct 26 '10 at 12:42
  • 1
    Because the SortOrders were not consecutive ( 3 and 4 are missing). This is what I was hoping to confirm by asking for data and desired output. – diceguyd30 Oct 26 '10 at 12:47
  • @DoctaJonez! @diceguyd30 has understood it correctly. – IsmailS Oct 26 '10 at 13:14

1 Answers1

6

Here's one possible LINQ answer, though I'm sure a more efficient one exists:

    inv
        .GroupBy(x => new { CustomerName = x.CustomerName, Date = x.Date })
        .SelectMany(x => x
                            .OrderBy(y => y.SortOrder)
                            .Select((y,i) => new { Value = y, Sort = y.SortOrder - i })
                            .GroupBy(y => y.Sort)
                            .Select(y => y.Select(z => z.Value))
        )
diceguyd30
  • 2,742
  • 20
  • 18