GaGa迷失的.NET

MY .NET

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

字符串(String)

LINQ to SQL支持以下String方法。但是不同的是默认情况下System.String方法区分大小写。而SQL则不区分大小写。

1.字符串串联(String Concatenation)

var q =
    from c in db.Customers
    select new
    {
        c.CustomerID,
        Location = c.City + ", " + c.Country
    };

语句描述:这个例子使用+运算符在形成经计算得出的客户Location值过程中将字符串字段和字符串串联在一起。

2.String.Length

var q =
    from p in db.Products
    where p.ProductName.Length < 10
    select p;

语句描述:这个例子使用Length属性查找名称短于10个字符的所有产品。

3.String.Contains(substring)

var q =
    from c in db.Customers
    where c.ContactName.Contains("Anders")
    select c;

语句描述:这个例子使用Contains方法查找所有其联系人姓名中包含“Anders”的客户。

4.String.IndexOf(substring)

var q =
    from c in db.Customers
    select new
    {
        c.ContactName,
        SpacePos = c.ContactName.IndexOf(" ")
    };

语句描述:这个例子使用IndexOf方法查找每个客户联系人姓名中出现第一个空格的位置。

5.String.StartsWith(prefix)

var q =
    from c in db.Customers
    where c.ContactName.StartsWith("Maria")
    select c;

语句描述:这个例子使用StartsWith方法查找联系人姓名以“Maria”开头的客户。

6.String.EndsWith(suffix)

var q =
    from c in db.Customers
    where c.ContactName.EndsWith("Anders")
    select c;

语句描述:这个例子使用EndsWith方法查找联系人姓名以“Anders”结尾的客户。

7.String.Substring(start)

var q =
    from p in db.Products
    select p.ProductName.Substring(3);

语句描述:这个例子使用Substring方法返回产品名称中从第四个字母开始的部分。

8.String.Substring(start, length)

var q =
    from e in db.Employees
    where e.HomePhone.Substring(6, 3) == "555"
    select e;

语句描述:这个例子使用Substring方法查找家庭电话号码第七位到第九位是“555”的雇员。

posted on 2009-11-10 13:22  为你疯狂  阅读(889)  评论(0编辑  收藏  举报