Subset of List of Lists based on Property

  • Thread starter Thread starter Marduk87RP
  • Start date Start date
M

Marduk87RP

Guest
I have this situation:

List<X> my_list;

public class X
{
public Y various{ get; set; }

public List<S> things{ get; set; }
}

public class S
{
public int id {get;set;}
....
}



I have this situation:

List<X> my_list;

public class X
{
public Y various{ get; set; }

public List<S> things{ get; set; }
}

public class S
{
public int id {get;set;}
....
}


I have to create a sublist of my_list based on the range of elements of the property, that is:

List<X> my_list:
[0]: various
things:
[0]: id,
[1]: id,
[2]: id,
[3]: id,
[4]: id
[1]: various
things:
[0]: id,
[1]: id,
[2]: id,
[3]: id,
[2]: various
things:
[0]: id,
[1]: id,
[2]: id,
[3]: id,

After the expected result are(if range is between 0-6):

List<X> final_list:
[0]: various
things:
[0]: id,
[1]: id,
[2]: id,
[3]: id,
[4]: id
[1]: various
things:
[0]: id,
[1]: id

Until now I have done this, I extracted the ids from the subset and then with this I wanted to select the elements of the main list whose internal list has the id value of those I extracted:


var iDs = my_list.SelectMany(x => x.things).ToList().GetRange(0, 6).Select(x => x.id).ToList();

List<X> temp= my_list;

foreach (var o in temp.SelectMany(x => x.things).ToList())
{
if (!iDs.Contains(o.id))
{

my_list.SelectMany(x => x.things).ToList().Remove(p);
}
}



Is my approach right,because the list has not changed? If yes I can't do what I described above, otherwise is there a better way to do what I'm looking for?

Continue reading...
 
Back
Top