释放器图片:Linq to SQL 字符串操作 - 蘭臺星光 - foxcommander - 和讯博...

来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 13:35:08
Linq to SQL 字符串操作 [转贴 2011-05-26 13:48:53]      

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”的雇员。

 

9.String.ToUpper()

var q =

  from e in db.Employees

  select new

  {

    LastName = e.LastName.ToUpper(),

    e.FirstName

  };

 

语句描述:这个例子使用ToUpper方法返回姓氏已转换为大 写的雇员姓名。

 

10.String.ToLower()

var q =

  from c in db.Categories

  select c.CategoryName.ToLower();

 

语 句描述:这个例子使用ToLower方法返回已转换为小写的类别名称。

 

11.String.Trim()

var q =

  from e in db.Employees

  select e.HomePhone.Substring(0, 5).Trim ();

 

语句描述:这个例子使用Trim方法返回雇员家庭电话号码的前五 位,并移除前导和尾随空格。

 

12.String.Insert(pos, str)

var q =

  from e in db.Employees

  where e.HomePhone.Substring(4, 1) == ")"

  select e.HomePhone.Insert(5, ":");

 

语句描述:这个例子使用 Insert方法返回第五位为 ) 的雇员电话号码的序列,并在 ) 后面插入一个 :。

 

13.String.Remove(start)

var q =

  from e in db.Employees

  where e.HomePhone.Substring(4, 1) == ") "

  select e.HomePhone.Remove(9);

 

语句描述:这个 例子使用Remove方法返回第五位为 ) 的雇员电话号码的序列,并移除从第十个 字符开始的所有字符。

 

14.String.Remove(start, length)

var q =

  from e in db.Employees

  where e.HomePhone.Substring(4, 1) == ")"

  select e.HomePhone.Remove(0, 6);

 

语句描述:这个例子使用Remove方法返 回第五位为 ) 的雇员电话号码的序列,并移除前六个字符。

 

15.String.Replace(find, replace)

var q =

  from s in db.Suppliers

  select new

  {

     s.CompanyName,

    Country = s.Country

    .Replace ("UK", "United Kingdom")

    .Replace ("USA", "United States of America")

   };

 

语句描述:这个例子使用 Replace 方法返回 Country 字段中UK 被替换为 United Kingdom 以及USA 被替换为 United States of America 的供 应商信息。

我最近在玩和讯微博,很方便,很实用,你也来和我一起玩吧!
去看看我的微博吧!http://t.hexun.com/4194019/default.html

/*0324 BlogMore*/img{ border:none}.moreArticles {margin-top:50px; font-size:14px;}.moreArticles ul{ list-style:disc;}.moreArticles ul li{ height:23px; line-height:23px; font-size:14px;}.moreArticles h2 { margin:0px; padding:0px; font-size:14px; font-weight:normal; height:30px; }.moreArticles h2 a{ text-decoration:underline}.conBg_oth{ position:absolute; left:505px; top:505px; width:644px; height:390px; border:1px solid #6d6b6a; z-index:900; background:#fff;}.editMya{ width:642px; height:390px; border:1px solid #a8bac1;font-size:12px;}.editTop{ background:url(../images/box_top_bg.jpg) repeat-x; height:22px; padding-top:3px; padding-left:12px; font-size:14px; font-weight:normal; color:#4e7786;}.editTop span{ float:right; padding-right:6px; padding-top:2px;}.editLeft{ width:396px; float:left; padding-left:12px; padding-top:12px; font-size:12px;}.f000{ color:#000;}.selectAticle{ height:25px;}.selectAticle .fright{ float:right}/*0324 BlogMore*/我的更多文章
  • SQL获取所有用户名,数据库名、所有表名、所有字段名及字段类型(转)
  • 在GrieView1中嵌套控件DataList的代码
  • 在C#.net中如何操作XML
  • C# 获取图片的大小和分辨率
  • LINQ to SQL语法