Archive for the ‘linq’ Tag
wildcard searches using LINQ
recently work on ASP.Net Project.
use LINQ as my Query Language, this is my favorite feature of .Net 3.5.
original I write my Search Query like this:
var searchQuery=from a in db.customer
where a.ID=param
select a;
this is not a wildcard search query , you only can get the result when parameter(search key word) exactly same with database record. So far customer no complain the search engine, because they don’t know and my boss also din ask me to do wildcard searches. but as a programmer, you do not want to create a system without standard. My boss always blame me do redundant work, customer won’t appreciate it. But I not care, just a minor change only.
to do wildcard searches in sql is use LIKE operation and wildcard characters( %), I believe you familiar than me if you still using ADO.NET to query your database. it something like this:
SELECT *FROM CustomersWHERE Phone LIKE '%param%'It's fairly simple, if you using LINQ, you just need to know .StartsWith, .EndsWith, and .Contains this 3 syntaxes. below show comparison of LINQ and SQL:
|
LINQ |
SQL |
|
| StartsWith |
var query=from a in db.customer
where a.ID.StartsWith(param) select a; |
Select*
from Customer where id=param%; |
| EndsWith |
var query=from a in db.customer
where a.ID.EndsWith(param) select a; |
Select*
from Customer where id=%param; |
| Contains |
var query=from a in db.customer
where a.ID.Contains(param) select a; |
Select*
from Customer where id=%param%; |
it look longer than SQL, but you don’t forget when you do binding in .net project you still need write very very long ADO.NET statement. Compare to ADO.NET, LINQ(actually also part of ADO.NET) is simply and easy, just 3 sentence to create bindable query.
So Far, I just know this method to do wildcard search, any better suggestion?
C# 3.0 Extension methods
Extension methods is a new features of C# 3.0, it allow developers to add new methods to a existing type(string, int, object and etc) and without change the definition of the original type.
Simple Extension Method Example:
let said we wanted to checking to see whether a int is odd? normally we can using function(make it reusable).
protected void Page_Load(object sender, EventArgs e) { int x=3; Response.Write(funcIsOdd(x)); } protected bool funIsOdd(int i) { bool ret; if (i % 2 != 0) ret= true; else ret= false; return ret; } |
by using the extension method in C# 3.0, we can instead add a IsOdd () method onto the int class itself. we define a new static class with a static method IsOdd() like below:
public static class Class1 { public static bool IsOdd(this int i) { if (i % 2 != 0) return true; else return false; } |
Notice the argument list of these function, especially keyword this. Keyword this followed by type tells to compilator that this method is applied to specified type.
after I defined the extension method, when hit the “.” keyword on a int variable, my extension methods will now show up in the IntelliSense drop-downlist:
评论 (2)