1

Excuse me, How I can get this result in linq? Some tips?

List<Test> listTests = new List<Test>();

var item1 = new Test() { NumberFac = 915170958, Description = "aa" };

var item2 = new Test() { NumberFac = 915170956, Description = "bb" };

var item3 = new Test() { NumberFac = 915176287, Description = "cc" };

var item4 = new Test() { NumberFac = 915170956, Description = "dd" };

var item5 = new Test() { NumberFac = 915176287, Description = "aa" };

var item6 = new Test() { NumberFac = 915170958, Description = "mm" };

var item7 = new Test() { NumberFac = 915176287, Description = "ll" };

listTests.Add(item1);

listTests.Add(item2);

listTests.Add(item3);

listTests.Add(item4);

listTests.Add(item5);

listTests.Add(item6);

listTests.Add(item7);

Result

 #Num |NumberFac|Description

   1   |915170958| aa

   2   |915170958| mm

   1   |915170956| bb

   2   |915170956| dd

   1   |915176287| cc

   2   | 915176287| aa

   3   |915176287 | ll  
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
amoralesu
  • 43
  • 4
  • What have you tried so far. We're here to help. Show some attempt at solving the problem. – Nkosi Dec 30 '15 at 00:14
  • 1
    Have you tried [this](http://stackoverflow.com/questions/2318885/multiple-order-by-with-linq)? – Jasen Dec 30 '15 at 00:15

1 Answers1

1

Here you go

var result = listTests
    .GroupBy(e => e.NumberFac)
    .SelectMany(g => g.Select((e, i) => new { Num = i + 1, e.NumberFac, e.Description }))
    .ToList();

First, GroupBy is used to group items by NumberFac, then SelectMany to flatten the result, and a special overload of Select that provides element index for producing the #Num inside the group.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343