第十二节: Linq用法大全(三)

一. SqlMethods操作

 1. Like

  自定义的通配表达式。%表示零长度或任意长度的字符串;_表示一个字符;[]表示在某范围区间的一个字符;[^]表示不在某范围区间的一个字符。

(1). 例如:查询消费者ID以“C”开头的消费者。

var q = from c in db.Customers
        where SqlMethods.Like(c.CustomerID,"C%")
        select c;

(2). 例如:查询消费ID不是“AXOXT”形式的消费者。

var q = from c in db.Customers
        where !SqlMethods.Like(c.CustomerID,"A_O_T")
        select c;

2. DateDiffDay

  求两个变量之间的差,DateDiffDay精确到天,类似还有:DateDiffHour、DateDiffMillisecond、DateDiffMinute、DateDiffMonth、DateDiffSecond、DateDiffYear。

例如:求订单下单后10天内发货的所有订单。

var q = from o in db.Orders
        where SqlMethods.DateDiffDay(o.OrderDate,o.ShippedDate) < 10
        select o;

 特别注意:上述代码中的SqlMethods来源于程序集System.Data.Linq,仅支持.Net ,不支持.Net Core。 Core中这么用:EF.Function.Like(),代码如下:

 var data3 = (from p in dbContext.T_UserInfor
              where EF.Functions.Like(p.userName, "%p%")
              select p).ToList();

 

二. Null语义和DateTime

1. Null语义

(1). Null

例如:查找不隶属于另一个雇员的所有雇员

var q =
    from e in db.Employees
    where e.ReportsToEmployee == null
    select e;

2. Nullable<T>.HasValue

例如:查找不隶属于另一个雇员的所有雇员

var q =
    from e in db.Employees
    where !e.ReportsTo.HasValue
    select e;

3. Nullable<T>.Value

例如:返回前者的EmployeeID 编号。请注意.Value 为可选

var q =
    from e in db.Employees
    where e.ReportsTo.HasValue
    select new
    {
        e.FirstName,
        e.LastName,
        ReportsTo = e.ReportsTo.Value
    };

2. DateTime

(1). 获取年(.Value.Year)

例如:获取2019年所有的订单

var q = from o in db.Orders
        where o.orderDate.Value.Year ==2019
        select o;

(2). 获取月(.Value.Month)

例如:获取所有年份中12月份中所有的订单

var q = from o in db.Orders
        where o.orderDate.Value.Month==12
        select o;

(3). 获取日(.Value.Day)

例如:获取所有年份所有月份20日下的所有订单

var q = from o in db.Orders
        where o.orderDate.Value.Day==20
        select o;

 

三. String相关

1. 字符串串联

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

 2. String.Length

 例如:查找名称短于10个字符的所有产品。

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

3. String.Contains

 例如:方法查找所有其联系人姓名中包含“Anders”的客户

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

4. String.IndexOf

例如:查找每个客户联系人姓名中出现第一个空格的位置。

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

5. String.StartsWith

 例如:查找联系人姓名以“Maria”开头的客户

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

6. String.EndsWith

例如:查找联系人姓名以“Anders”结尾的客户 

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

7. String.SubString

(1) 返回产品名称中从第四个字母开始往后的部分

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

(2) 法查找家庭电话号码第七位到第九位是“555”的雇员。 Substring(起始位置,长度)

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

8. ToUpper和ToLower

var q =
    from e in db.Employees
    select new
    {
        LastName = e.LastName.ToUpper(),
        FirstName = e.FirstName.ToLower()
    };

9. Trim

例如: 返回雇员家庭电话号码的前五位,并移除前导和尾随空格

var q =
    from e in db.Employees
    select e.HomePhone.Substring(0, 5).Trim();

10. Insert

 例如:返回第五位为 ) 的雇员电话号码的序列,并在 ) 后面插入一个 :。

var q =
    from e in db.Employees
    where e.HomePhone.Substring(4, 1) == ")"
    select e.HomePhone.Insert(5, ":");

11. Remove

(1). 返回第五位为 ) 的雇员电话号码的序列,并移除从第十个字符开始往后的所有字符

var q =
    from e in db.Employees
    where e.HomePhone.Substring(4, 1) == ")"
    select e.HomePhone.Remove(9);

(2).返回第五位为 ) 的雇员电话号码的序列,并移除前六个字符

var q =
    from e in db.Employees
    where e.HomePhone.Substring(4, 1) == ")"
    select e.HomePhone.Remove(0, 6);

12. Replace

 例如:返回 Country 字段中其中把UK替换为 United Kingdom 以及USA 被替换为 United States of America 的供应商信息

var q =
    from s in db.Suppliers
    select new
    {
        s.CompanyName,
        Country = s.Country
        .Replace("UK", "United Kingdom")
        .Replace("USA", "United States of America")
    };

 

四. 运算符转换

1. ToArray: 将序列转换为数组

例如:查询出所有的顾客信息放到数组里 

var q = from c in db.Customers
            select c;
Customer[] cArray= q.ToArray();

2. ToList: 将序列转为泛型List

 例如:查询出所有的顾客信息放到List里

var q = from c in db.Customers
            select c;
List<Customers> cList= q.ToList();

3. ToDictionary:将序列转换成字典

 例如:查询出所有顾客信息放到字典中。

var q = from c in db.Customers
        select c;
Dictionary<int, Customers> cDict= q.ToDictionary(p=>p.cId);

PS:以上代码表是以顾客的id当作key,该id对应的Customers当作Value进行存储。

 

 

 

 

!

  • 作       者 : Yaopengfei(姚鹏飞)
  • 博客地址 : http://www.cnblogs.com/yaopengfei/
  • 声     明1 : 如有错误,欢迎讨论,请勿谩骂^_^。
  • 声     明2 : 原创博客请在转载时保留原文链接或在文章开头加上本人博客地址,否则保留追究法律责任的权利。
 
posted @ 2019-11-06 16:10  Yaopengfei  阅读(939)  评论(2编辑  收藏  举报