views:

141

answers:

4

I need to get the list of records that does not starts with alphabets. i.e. Which starts with numeric and any special character.

I am using asp.net mvc (C#) for my application.

Whats the simple linq query to get this list?

A: 
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" */
Svisstack
+2  A: 

Something like this?

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 using Regex here was overkill and my solution wasn't perfect anyway.

Aamir
+1  A: 
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 could hydrate your retrieval from the database by by enumerating the query (call ToList). From that point on, your operations will be against in-memory objects and those 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()));
Nicholas Murray
A: 

One thing to note is that you don't need to convert the string to a chararray to use linq on it.

The more consise version would be:

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

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