DevExpress DXperience 的ASPxFilterControl 组件使用 Linq 模糊查找 Oracle 字符字段不区分大小写终极解决方案

DevExpress 的demo都是用Access及SQL Server做数据库示例,查找字符串都是不区分大小写,但改为用Linq + Oracle后,用户的不区分大小写的模糊匹配查找习惯就跟难满足。Baidu,Google搜索不到答案,DevExpress.com也没相关Q&A,于是琢磨了多天,终于想出完美方案,示例如下:

新建一个WebForm,名称为User.aspx,在窗体中添加一个ASPxFilterControl组件,一个Button及一个GridView

在 Page_Load中加入筛选条件字段,按钮加上筛选事件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="User.aspx.cs" Inherits="User" %>



<%@ Register assembly="DevExpress.Web.v13.1, Version=13.1.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    

    </div>

    <dx:ASPxFilterControl ID="ASPxFilterControl1" runat="server">

    </dx:ASPxFilterControl>

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Filter" />

    <br />

    <asp:GridView ID="GridView1" runat="server">

    </asp:GridView>

    </form>

</body>

</html>
 1 using System;

 2 using System.Diagnostics;

 3 using System.Linq;

 4 using System.Web.Configuration;

 5 using DevExpress.Data.Filtering;

 6 using DevExpress.Web.ASPxEditors;

 7 using DevExpressEx.Data.Linq.Helpers;

 8 using Model;

 9 

10 public partial class User : System.Web.UI.Page

11 {

12     public Model1Container db = new Model1Container(WebConfigurationManager.ConnectionStrings["OrclConnString"].ToString());

13 

14     protected void Page_Load(object sender, EventArgs e)

15     {

16         if (!IsPostBack)

17         {  

18             FilterControlColumn fcc = new FilterControlTextColumn();

19             fcc.PropertyName = "USER_ID";

20             fcc.DisplayName = "UserID";

21             ASPxFilterControl1.Columns.Add(fcc);

22 

23             FilterControlColumn fcc2 = new FilterControlTextColumn();

24             fcc2.PropertyName = "EMAIL";

25             fcc2.DisplayName = "Email";

26             ASPxFilterControl1.Columns.Add(fcc2);

27         }

28 

29     }

30     protected void Button1_Click(object sender, EventArgs e)

31     {

32         BindData();

33     }

34 

35     public void BindData()

36     {

37         CriteriaOperator coFilter = CriteriaOperator.Parse(ASPxFilterControl1.FilterExpression);

38 

39         IQueryable iqQuery = (from u in db.USER_INFO orderby u.USER_ID

40                    select new

41                    {

42                        u.USER_ID,

43                        u.EMAIL,

44                    }).Where(coFilter).Take(10);

45 

46         string sSQL = (iqQuery as System.Data.Objects.ObjectQuery).ToTraceString();

47         Debug.WriteLine(sSQL);

48 

49         GridView1.DataSource = iqQuery;

50         GridView1.DataBind();

51     }

52 

53 }

现在执行程序,查找Email中含有test的用户,全大写,全小写,大小写混合的相关记录都显示。测试成功。

DevExpress DXperience 的ASPxFilterControl 组件使用 Linq 模糊查找 Oracle 字符字段不区分大小写终极解决方案
通过
        string sSQL = (iqQuery as System.Data.Objects.ObjectQuery).ToTraceString();
        Debug.WriteLine(sSQL);
代码可查看到生成的Oracle 语句已含有区分大小写

SELECT * 

FROM ( 

SELECT 

"Project1"."C1" AS "C1", 

"Project1"."USER_ID" AS "USER_ID", 

"Project1"."EMAIL" AS "EMAIL"

FROM ( SELECT 

    "Extent1"."USER_ID" AS "USER_ID", 

    "Extent1"."EMAIL" AS "EMAIL", 

    1 AS "C1"

    FROM "XIMEI"."USER_INFO" "Extent1"

    WHERE (LOWER("Extent1"."EMAIL") LIKE '%test%')

)  "Project1"

ORDER BY "Project1"."USER_ID" ASC

)

WHERE (ROWNUM <= (10) )

 

关于 using DevExpressEx.Data.Linq.Helpers; 有兴趣的朋友可到csdn.net 下载

关于ASPxGridView 组件使用 Linq 模糊查找 Oracle 字符字段不区分大小写解决方案,我会尽快抽空完成


 

原创文章,转载请注明出处~http://www.cnblogs.com/fiozhao/p/3517028.html

你可能感兴趣的:(DevExpress)