LINQ - BETWEEN "a" AND "e"

I have a general list of students. I want to filter the student name begins between "a" and "e"with using LINQ.

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

Please help me do this.

+4
source share
10 answers

Untethered by this may work:

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

As an alternative

Students = Students.Where(s => ["a", "b", "c", "d", "e"]
                               .contains(s.Name[0]).ToList();
+5
source

The range is defined less / more, and not just ==:

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

You can also use lowercase letters or compare them with both cases.

+4
source

. :

, :

Students = Students.Where(s => s.Name[0] >= 'a' && s.Name[0] <= 'e');

ASCII:

Students = Students.Where(s => s.Name[0] >= 97 && s.Name[0] <= 101);

, Substring Name[0], char/int string.


, , , - , :

string[] chars = { "a", "b", "c", "d", "e" };
Students = Students.Where(s => chars.Contains(s.Name.Substring(0, 1)));
+4

, , ints whos ASCII :

private static bool StartsWithRange(string value, char first, char last)
{
  if (string.IsNullOrEmpty(value))
  {
    return false;
  }

  if (first > last)
  {
    throw new ArgumentException(string.Format("'{0}' shouldn't come after '{1}'.", first, last), nameof(last));
  }

  int intValue = value.ToLower()[0];
  return intValue >= first && intValue <= last;
}

:

Students.Where(s => StartsWithRange(s, 'a', 'e')).ToList();

, , :

Assert.IsFalse(StartsWithRange("abcd", 'd', 'e'));
Assert.IsTrue(StartsWithRange("dcd", 'd', 'e'));
Assert.IsTrue(StartsWithRange("Dcd", 'd', 'e'));
Assert.IsTrue(StartsWithRange("ebcd", 'd', 'e'));
Assert.IsTrue(StartsWithRange("Ebcd", 'd', 'e'));
Assert.IsFalse(StartsWithRange("fbcd", 'd', 'e'));
Assert.IsFalse(StartsWithRange("Fbcd", 'd', 'e'));
Assert.IsFalse(StartsWithRange(string.Empty, 'd', 'e'));

ToLower -call, , value .

, , "" , , .

+4

.

students = students.Where(x => Regex.IsMatch(x.Name, @"^[a-e].*", RegexOptions.IgnoreCase) == true).ToList();

, @"^[a-e].*", , , a,b,c,d,e. , , .

. , using System.Text.RegularExpressions;, .

+4

, .

var s = new [] {"a","b","c","d","e"};
Students = Students.Where(s => s.Contains(s.Name.Substring(0, 1))).ToList();
+3

, .

Students = Students.Where(s => {
   char c = s.Name[0];
   return (c >= 'a' && c <= 'e') || (c >= 'A' && c <= 'E') ;
}).ToList();

.

+2

:

var filter = Students.Where(s => s!= null && s [0] >= 'a' && s[0] <= 'e').ToList();
+2

string.CompareOrdinal.

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

It will include "a" and "e". You can use uppercase conversion before comparison also for case-insensitive comparisons.

+2
source

I would consider using the Between Extensions method and using it.

Code example:

    public void Main()
    {
        var names = new string[] { "andy", "lisa", "zoro", "billy" };

        var result = FilterBetween(names, 'a', 'e');
    }

    public IEnumerable<string> FilterBetween(IEnumerable<string> names, char start, char end)
    {
        return names.
            Where(name => name.
                First().
                Between(start, end));
    }



public static class ExtensionMethods
{
    public static bool Between<T>(this T actual, T lower, T upper) where T : IComparable<T>
    {
        return actual.CompareTo(lower) >= 0 && actual.CompareTo(upper) <= 0;
    }
}
+2
source

Source: https://habr.com/ru/post/1612349/


All Articles