LinqToSQL2

Extension method:

Extension method is a new feature of C # 3.0, you can come to the purpose of expansion by adding new types of methods are known types, but does not need to make any changes of this type.

Key to remember is defined as extension methods static methods can only be called upon to display the namespace into your application code by using instructions.

LINQ extension contains the most common method, also known as the standard query operators. Standard query operators extends IEnumerable <T> and IQueryable two types.

Examples of code required to call a method extension method, and then the intermediate language (IL) produced by the compiler will be transcoded static method call.

 If you must use an extension method, keep in mind the following points:

1. If the method of extension methods defined in the same type that already exists with the same name, then the extension method will never be called. (Method Do the same name).

2. extension methods can only be introduced at the level of the namespace and to use.

Code offer (an upper bound)

public static class MyExtensions
{
public static bool IsValidEmail(this string email)
{
Regex exp = new Regex(@"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$");
return exp.IsMatch(email);
}
}

 

Context = new new the DataContext the DataContext (the connectionString);
the Table <Contack> Contacts context.GetTable = <Contack> ();
var = Query in contacts.AsEnumerable from S ()
SELECT new new s.EmailAddress {};
the foreach (var Item in Query)
{
IF (item.EmailAddress.IsValidEmail ()) // call here a bit extension method written above
{
System.Console.WriteLine (item.EmailAddress); // here is Filtered Email
}

}

 

Guess you like

Origin www.cnblogs.com/jxl123456/p/11108900.html