LINQ to SQL語句(15)之String

作者: 李永京  來源: 博客園  發布時間: 2008-09-18 16:50  閱讀: 10582 次  推薦: 2   原文鏈接   [收藏]  
 
摘要:這個系列的第十五篇,講解String用法。
[1] String講解1
[2] String講解2

系列文章導航:

LINQ to SQL語句(1)之Where

LINQ to SQL語句(2)之Select/Distinct

LINQ to SQL語句(3)之Count/Sum/Min/Max/Avg

LINQ to SQL語句(4)之Join

LINQ to SQL語句(5)之Order By

LINQ to SQL語句(6)之Group By/Having

LINQ to SQL語句(7)之Exists/In/Any/All/Contains

LINQ to SQL語句(8)之Concat/Union/Intersect/Except

LINQ to SQL語句(9)之Top/Bottom和Paging和SqlMethods

LINQ to SQL語句(10)之Insert

LINQ to SQL語句(11)之Update

LINQ to SQL語句(12)之Delete和使用Attach

LINQ to SQL語句(13)之開放式并發控制和事務

LINQ to SQL語句(14)之Null語義和DateTime

LINQ to SQL語句(15)之String

LINQ to SQL語句(16)之對象標識

LINQ to SQL語句(17)之對象加載

LINQ to SQL語句(18)之運算符轉換

LINQ to SQL語句(19)之ADO.NET與LINQ to SQL

LINQ to SQL語句(20)之存儲過程

LINQ to SQL語句(21)之用戶定義函數

LINQ to SQL語句(22)之DataContext

LINQ to SQL語句(23)之動態查詢

LINQ to SQL語句(24)之視圖

LINQ to SQL語句(25)之繼承

LINQ簡介

adsfsaf

 

字符串(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”的雇員。

[第1頁][第2頁]
2
0
 
標簽:LINQ LINQ to SQL
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()