LINQ to SQL语句(15)之String

本文介绍了LINQtoSQL中支持的多种字符串处理方法,包括字符串串联、长度获取、子串搜索等,并通过实例展示了如何在查询表达式中使用这些方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载地址: http://www.prg-cn.com/article-4446-1.html

字符串(String)

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

1.字符 串串联(String Concatenation)

  1. var q =
  2.   from c in db.Customers
  3.   select new
  4.   {
  5.      c.CustomerID,
  6.     Location = c.City + ", " + c.Country
  7.   };
复制代码

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

2.String.Length

  1. var q =
  2.   from p in db.Products
  3.   where p.ProductName.Length < 10
  4.   select p;
复制代码

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

3.String.Contains(substring)

  1. var q =
  2.   from c in db.Customers
  3.   where c.ContactName.Contains ("Anders")
  4.   select c;
复制代码

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

4.String.IndexOf(substring)

  1. var q =
  2.   from c in db.Customers
  3.   select new
  4.   {
  5.      c.ContactName,
  6.     SpacePos = c.ContactName.IndexOf(" ")
  7.   };
复制代码

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

5.String.StartsWith (prefix)

  1. var q =
  2.   from c in db.Customers
  3.   where c.ContactName.StartsWith("Maria")
  4.   select c;
复制代码

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

6.String.EndsWith(suffix)

  1. var q =
  2.   from c in db.Customers
  3.   where c.ContactName.EndsWith("Anders")
  4.   select c;
复制代码

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

7.String.Substring(start)

  1. var q =
  2.   from p in db.Products
  3.   select p.ProductName.Substring(3);
复制代码

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

8.String.Substring (start, length)

  1. var q =
  2.   from e in db.Employees
  3.    where e.HomePhone.Substring(6, 3) == "555"
  4.   select e;
复制代码

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

9.String.ToUpper()

  1. var q =
  2.   from e in db.Employees
  3.   select new
  4.   {
  5.     LastName = e.LastName.ToUpper(),
  6.     e.FirstName
  7.   };
复制代码

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

10.String.ToLower()

  1. var q =
  2.   from c in db.Categories
  3.   select c.CategoryName.ToLower();
复制代码

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

11.String.Trim()

  1. var q =
  2.   from e in db.Employees
  3.   select e.HomePhone.Substring(0, 5).Trim ();
复制代码

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

12.String.Insert(pos, str)

  1. var q =
  2.   from e in db.Employees
  3.   where e.HomePhone.Substring(4, 1) == ")"
  4.   select e.HomePhone.Insert(5, ":");
复制代码

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

13.String.Remove(start)

  1. var q =
  2.   from e in db.Employees
  3.   where e.HomePhone.Substring(4, 1) == ") "
  4.   select e.HomePhone.Remove(9);
复制代码

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

14.String.Remove(start, length)

  1. var q =
  2.   from e in db.Employees
  3.   where e.HomePhone.Substring(4, 1) == ")"
  4.   select e.HomePhone.Remove(0, 6);
复制代码

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

15.String.Replace(find, replace)

  1. var q =
  2.   from s in db.Suppliers
  3.   select new
  4.   {
  5.      s.CompanyName,
  6.     Country = s.Country
  7.     .Replace ("UK", "United Kingdom")
  8.     .Replace ("USA", "United States of America")
  9.    };
复制代码

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值