text = wordcount.ToString();
Run the project and click the button. The text box shows the value 10.
Let??™s expand this a little. In every application I??™ve worked on, there??™s been a need to validate email
addresses. Here??™s how to do that easily with extension methods. Modify the extension method code by
adding the following highlighted code:
namespace MyExtensionMethods
{
public static class MyExtensions
{
public static int GetWordCount(this System.String mystring)
{
return mystring.Split(null).Length;
}
public static bool IsValidEmail(this string email)
{
Regex exp = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return exp.IsMatch(email);
}
}
}
Next, replace the code behind the button with the following:
DataContext context =
new DataContext("Initial Catalog=AdventureWorks;Integrated Security=sspi");
Table
contact = context.GetTable();
var query =
from c in contact
select new { c.EmailAddress} ;
foreach (var item in query)
if (item.EmailAddress.IsValidEmail())
{
listbox1.Items.Add(item.EmailAddress);
}
27
Part I: Introduction to Project LINQ
Just like the first example, this one simply adds a method onto the string class that validates email
addresses.
Pages:
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77