0

I have a list of arrays like this:

List<string[]> definitions

  MATERIAL  "STEEL"    TYPE "Steel"    WEIGHTPERVOLUME 7833.414
  MATERIAL  "STEEL"    SYMTYPE "Isotropic"  E 2.038902E+10  
  MATERIAL  "STEEL"  FY 3.515348E+07  FU 4.569952E+07 
  MATERIAL  "STEEL"  HYSTYPE "Kinematic"  SSTYPE "Simple"   
  MATERIAL  "CONC"    TYPE "Concrete"    WEIGHTPERVOLUME 2402.616
  MATERIAL  "CONC"    SYMTYPE "Isotropic"  E 2.531051E+09  U 0.2
  MATERIAL  "CONC"    FC 2812279
  MATERIAL  "CONC"  HYSTYPE "Concrete"  ENERGYDEGFACT 0
  MATERIAL  "OTHER"    TYPE "Other"    WEIGHTPERVOLUME 7833.414
  MATERIAL  "OTHER"    SYMTYPE "Isotropic"  E 2.038902E+10
  MATERIAL  "OTHER"  DESIGNTYPE "OTHER"  
  MATERIAL  "OTHER"  HYSTYPE "Elastic"  IOTENSION 0.01
  MATERIAL  "OTHER"  STRAIN -1 STRESS -1 
  MATERIAL  "OTHER"  STRAIN 0 STRESS 0 POINTID "A"  
  MATERIAL  "OTHER"  STRAIN 1 STRESS 1 

I want to be able create a list like this out of the definitions list:

List<List<string[]>> objects

and separate the lists based on the second element in each array:

  MATERIAL  "STEEL"    TYPE "Steel"    WEIGHTPERVOLUME 7833.414
  MATERIAL  "STEEL"    SYMTYPE "Isotropic"  E 2.038902E+10  
  MATERIAL  "STEEL"  FY 3.515348E+07  FU 4.569952E+07 
  MATERIAL  "STEEL"  HYSTYPE "Kinematic"  SSTYPE "Simple"  


  MATERIAL  "CONC"    TYPE "Concrete"    WEIGHTPERVOLUME 2402.616
  MATERIAL  "CONC"    SYMTYPE "Isotropic"  E 2.531051E+09  U 0.2
  MATERIAL  "CONC"    FC 2812279
  MATERIAL  "CONC"  HYSTYPE "Concrete"  ENERGYDEGFACT 0


  MATERIAL  "OTHER"    TYPE "Other"    WEIGHTPERVOLUME 7833.414
  MATERIAL  "OTHER"    SYMTYPE "Isotropic"  E 2.038902E+10
  MATERIAL  "OTHER"  DESIGNTYPE "OTHER"  
  MATERIAL  "OTHER"  HYSTYPE "Elastic"  IOTENSION 0.01
  MATERIAL  "OTHER"  STRAIN -1 STRESS -1 
  MATERIAL  "OTHER"  STRAIN 0 STRESS 0 POINTID "A"  
  MATERIAL  "OTHER"  STRAIN 1 STRESS 1 
Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 1
    Can you show us the code you've tried thus far? –  Jan 22 '17 at 16:27
  • 1
    Sounds good. What have you tried so far? – David L Jan 22 '17 at 16:27
  • @ Amy I'm still trying! – Vahid Jan 22 '17 at 16:28
  • The data in your question is very difficult to parse so I've no idea what you're actually asking here. – DavidG Jan 22 '17 at 16:31
  • @DavidG I do not want to parse it actually right now. Just create a List of Lists containing string arrays. – Vahid Jan 22 '17 at 16:36
  • By parse I mean "I don't understand how the data you have posted fits into the data structure you say you have" – DavidG Jan 22 '17 at 16:37
  • I have split each line you see here in a single array of strings. For example: MATERIAL "STEEL" TYPE "Steel" WEIGHTPERVOLUME 7833.414 is array1 and so on. all of these arrays are in a list. Now what I want is to create another list that contains lists of arrays. And these inner lists contain these string arrays. The criteria for this separation is second element in each array! – Vahid Jan 22 '17 at 16:40

3 Answers3

0

Why not just use GroupBy?

var listOfGroups = definitions.GroupBy(x => x.Material);

You will get a list of elements, which has a Key and a list of the grouped objects.

Here is a similar question

Community
  • 1
  • 1
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
  • Thanks Alexey, I have arrays here in my list. And I want to be able to separate them based on the second element. STEEL or CONC for example- – Vahid Jan 22 '17 at 16:34
  • If you paste some code that actually initialises the lists (arrays) and shows the structure of them, it would be much easier. – Alexey Zimarev Jan 22 '17 at 16:36
0

Than just GroupBy second elemtn in array:

var groups = definitions.GroupBy(x => x[1]);

It's not a List what you get, it's IEnumerable<IGrouping<string, string[]>> But i think it's exact what you are looking for.

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
-2

Try using DataTable and using LINQ you can filter it

DataTable dt=new DataTable();
//Add the data in it
dt.Where() or dt.Select();
Sreemat
  • 616
  • 10
  • 28