Get records that don't start with an alphabetic character in linq

I need to get a list of entries that do not start with an alphabetic character, i.e. which starts with a numeric character or any special character.

What's a simple LINQ query to get this list?

+2


a source to share


4 answers


List<string> Entries = new List<string>();
Entries.Add("foo");
Entries.Add("bar");
Entries.Add("@foo");
Entries.Add("1bar");

var NonAlphas = (from n in Entries
where !char.IsLetter(n.ToCharArray().First())
select n);

      

For Linq-to-sql, you can take your search out of the database by enumerating the query (calling ToList). From now on, your actions will be against objects in memory, and these operations will not be translated into SQL.



List<string> Entries = dbContext.Entry.Where(n => n.EntryName).ToList();
var NonAlphas = Entries.Where(n => !char.IsLetter(n.First()));

      

+4


a source


Something like that?

List<string> lst = new List<string>();
lst.Add("first");
lst.Add("second");
lst.Add("third");
lst.Add("2abc");

var result = from i in lst where !char.IsLetter(i[0]) select i; 
List<string> output = result.ToList();

      



Edit: I realized that the use of Regex here was overkill and my solution was not perfect anyway.

+3


a source


string[] x = new string[3];
x[0] = "avb";
x[1] = "31df";
x[2] = "%dfg";     

var linq = from s in x where !char.IsLetter(s.ToString().First()) select s;
List<string> simplelist = new List<string>(linq);
/* in simple list you have only "31df" & "dfg" */

      

0


a source


It should be noted that you do not need to convert the string to chararray to use linq on it.

A more consistent version would be:

var list = new List<string> {"first","third","second","2abc"};

var result = list.Where(word => !char.IsLetter(word.First())); 

      

0


a source







All Articles