asp.net search database based on Chinese pinyin first letter (with LINQ call method) _ Practical Tips

Source: Internet
Author: User
Method One: First query all the records, and then the logic layer into the phonetic alphabet after the query, obviously a fool will do so.

Method Two: In the need to search the table to add a field for the content of the retrieved fields corresponding to the pinyin, in search of the same time to query these two fields, this method is feasible, but will increase the size of the database storage.

Method Three: Create a function in the database, in the execution of query statements through this function to transform the search, the function is as follows:

Copy Code code as follows:

SET ANSI_NULLS on
Go
SET QUOTED_IDENTIFIER ON
Go
-- =============================================
--Author:xoyozo
--Create DATE:2010-4-17
--Description: Provide first letter in Chinese
-- =============================================
CREATE FUNCTION fun_getpy
(
@str NVARCHAR (4000)
)
RETURNS NVARCHAR (4000)
As
BEGIN
DECLARE @word NCHAR (1), @PY NVARCHAR (4000)
SET @PY = '
While Len (@str) >0
BEGIN
SET @word =left (@str, 1)
SET @PY = @PY + (case when Unicode (@word) BETWEEN 19968 and 19968+20901
THEN (SELECT top 1 PY from (
SELECT ' A ' as Py,n ' 驁 ' as Word
UNION all SELECT ' B ', N ' book '
UNION all SELECT ' C ', N ' wrong '
UNION all SELECT ' D ', N ' 鵽 '
UNION all SELECT ' E ', N ' 樲 '
UNION all SELECT ' F ', N ' 鰒 '
UNION all SELECT ' G ', N ' hiker '
UNION all SELECT ' H ', N ' 夻 '
UNION all SELECT ' J ', N ' 攈 '
UNION all SELECT ' K ', N ' 穒 '
UNION all SELECT ' L ', N ' 鱳 '
UNION all SELECT ' M ', N ' temperature '
UNION all SELECT ' n ', n ' 桛 '
UNION all SELECT ' O ', N ' 漚 '
UNION all SELECT ' P ', N ' exposure '
UNION all SELECT ' Q ', N ' 囕 '
UNION all SELECT ' R ', N ' 鶸 '
UNION all SELECT ' S ', N ' 蜶 '
UNION all SELECT ' T ', N ' 籜 '
UNION all SELECT ' W ', N ' 鶩 '
UNION all SELECT ' X ', N ' 鑂 '
UNION all SELECT ' Y ', N ' rhythmic '
UNION all SELECT ' Z ', N ' out '
) T
WHERE word>= @word COLLATE chinese_prc_cs_as_ks_ws
ORDER by PY ASC) ELSE @word end)
SET @str =right (@str, Len (@str)-1)
End
Return @PY
End



How to use:
Copy Code code as follows:

SELECT * FROM table where fun_getpy (field) like N '%zgr% '



This is the quickest and easiest way to test the title first letter in the article table, which takes about 3 times times less than the search for this function, and believes that the cost of a project deployed in a small amount of data is very low.

LINQ mode operation
A novice friend of LINQ may not be very familiar with the. dbml file, the tables in the database can be dragged directly to the left of the. dbml, and stored procedures, functions, and so on can be dragged directly to the right, and then used directly in logic, here is an example:
Copy Code code as follows:

Dataclasses1datacontext db = new Dataclasses1datacontext ();
var q = from d in Db. Documents
where D.title.contains ("Zgr") | | Db.fun_getpy (D.title). Contains ("Zgr")
Select D;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.