Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ - BETWEEN "a" AND "e"

Tags:

c#

linq

I have a Generic List of Students. I want to filter the student name starts between "a" and "e" with the help of LINQ.

Students = Students.Where(s => s.Name.Substring(0, 1) == "a" 
                            && s.Name.Substring(0, 1) == "e").ToList();

Please help me to do that.

like image 359
Jesuraja Avatar asked Nov 28 '22 05:11

Jesuraja


2 Answers

Untested by this might work:

Students = Students.Where(s => s.Name.Substring(0, 1) >= "a" 
                        && s.Name.Substring(0, 1) <= "e").ToList();

Alternatively

Students = Students.Where(s => ["a", "b", "c", "d", "e"]
                               .contains(s.Name[0]).ToList();
like image 175
Prescott Avatar answered Dec 18 '22 01:12

Prescott


Range defined by less/greater, not by just ==:

 Students = Students.Where(s => s.Name.Substring(0, 1) >= "a" 
                        && s.Name.Substring(0, 1) <= "e").ToList();

You also may want to lowercase or compare to both cases.

like image 38
Alexei Levenkov Avatar answered Dec 17 '22 23:12

Alexei Levenkov