Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I was some nested list, and I would like to extract complexe information, but I don't know how write the linq expression.



C#
public class Section
{
      public List<Side> SideList {get; set; } = new List<Side>()
}

public class Side
{
      public List<PositionSide> PositionList {get; set; } = new List<PositionSide>()
}

public class PositionSide : 
{
      public int Position {get; set;}
      ....
}

I would like to extract the list of Side where the position which is above 0 and below 6 but when I do the request :
C#
List<side> listNozzleBySide = SectionList.SelectMany(x => x.SideList.Where(y => y.PositionList.Any(z => z.Position >= 1 && z.Position <=5))).ToList();


the position 0 and 6 rest present in the list of side.

for example I have this in the list

section 1 
 Side 1
  Position 0
  Position 1
  Position 2
  Position 3
  Position 4
  Position 5
  Position 6


I would like to have this :
Section 1
  Side 1
    Position 1
    Position 2
    Position 3
    Position 4
    Position 5


What I have tried:

C#
List<side> listNozzleBySide = SectionList.SelectMany(x => x.SideList.Where(y => y.PositionList.Any(z => z.Position >= 1 && z.Position <=5))).ToList();
Posted
Updated 8-Dec-19 23:41pm
v2
Comments
Richard MacCutchan 9-Dec-19 4:23am    
And? What result did your code produce?
F-ES Sitecore 9-Dec-19 4:43am    
"Any" just returns true if the condition inside is met. Your code is asking for the Side object where there are any positions >=1 1 and <=5. It isn't actually filtering the positions down to match that criteria, so your Side is returned with all positions intact.

After you get your list of Side objects just iterate through them and remove any that are not in the desired range.

Ignoring that your sample code won't compile - always check your code works and produces the problem in simplified form or we all waste a load of time working on bad code - Any is your problem.
Any returns a bool: true if any single member of the sequence matches the condition, false in no element of the sequence matches. So if any member is between 1 and 5 inclusive, all elements are valid and will be included in the output.
Probably, what you want is closer to this:
C#
var s1 = SectionList.Select(sections => sections.SideList.Select(sides => sides.PositionList.Where(ps => ps.Position >= 1 && ps.Position <= 5))).ToList();
But it's difficult to tell with what you have given us.
 
Share this answer
 
Quote:
I would like to extract the list of Side where the position which is above 0 and below 6 but when I do the request :
C#
List<side> listNozzleBySide = SectionList.SelectMany(x => x.SideList.Where(y => y.PositionList.Any(z => z.Position >= 1 && z.Position <=5))).ToList();

the position 0 and 6 rest present in the list of side.


Well... i'll try to explain it. Imagine, you've got 120 students in 4 classes (30 students in a class). You want to get students with red socks on their legs. Assuming that in each class there's at least one student with red socks, each class is meeting your criteria. Got it?
If NO, you have to get students instead of classes.

As to your example, you have to get positions insteads of sides.

Take a look at example:
C#
void Main()
{
	School school = new School(){Classes = new List<SchoolClass>()
		{
			new SchoolClass(){ClassID = 1, ClassName="A", Students = new List<Student>()
				{
					new Student(){StuID = 1, Name="Maciej", ColorOfSocks="yellow"},
					new Student(){StuID = 5, Name="Anna", ColorOfSocks="red"},
					new Student(){StuID = 22, Name="Robert", ColorOfSocks="green"}
				}},
			new SchoolClass(){ClassID = 2, ClassName="B", Students = new List<Student>()
				{
					new Student(){StuID = 2, Name="Fred", ColorOfSocks="black"},
					new Student(){StuID = 3, Name="Viana", ColorOfSocks="gray"},
					new Student(){StuID = 21, Name="Julia", ColorOfSocks="red"}
				}}
		}};
	
	var cla = school.Classes.Where(c=>c.Students.Any(s=>s.ColorOfSocks=="red"));
	//returns the list of all existing classes, because in each class there's at least one studnet with red socks!
	
	var stu = school.Classes.Select(c=>c.Students.Where(s=>s.ColorOfSocks=="red"));
	//returns the list of students which meets the criteria: {color of socks = red}
	
}

// Define other methods and classes here

public class School
{
	public List<SchoolClass> Classes = new List<SchoolClass>();
}

public class SchoolClass
{
    public int ClassID {get; set;}
	public string ClassName {get; set;}
	public List<Student> Students = new List<Student>();
}

public class Student
{
	public int StuID {get; set;}
	public string Name {get; set;}
	public string ColorOfSocks {get; set;}
	
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900