Solved

Enumerable.Select

Posted on 2010-09-21
2
408 Views
Last Modified: 2013-11-11
How to print only the digits from the ID field?
(without using basic substring and indexOf functions to check each ID)

            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(String));
            dt.Rows.Add("USB22345");
            dt.Rows.Add("USC33399");
            dt.Rows.Add("USD66777");

            var query = from c in dt.AsEnumerable()
            select c["ID"];

            foreach (string item in query)
                Console.WriteLine(item.Select(c => char.IsDigit(c)));

Open in new window

0
Comment
Question by:mawingpui
2 Comments
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 125 total points
ID: 33726094

            foreach (string item in query)

            {

                char[] numsArray = item.ToCharArray().Where(c => char.IsDigit(c)).ToArray();

                string numString = new StringBuilder().Append(numsArray).ToString();

                Console.WriteLine(numString);

            }

Open in new window

0
 
LVL 12

Accepted Solution

by:
kumar754 earned 125 total points
ID: 33726907
try this:

foreach (string item in query)
                Console.WriteLine(string.Join( null,System.Text.RegularExpressions.Regex.Split( item, "[^\\d]" ) ));
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
No row return after calling the fillschema method 4 39
Memory Usage 2 49
What is Thread.Sleep? 5 44
Syntax error 9 45
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now