All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
sql,server,2005中的output子句_[SQL,Server教程]
了解Hibernate的FlushMode.NEVER_.net资料_编程技术
VS2005之C#项目调试问题解决方案集锦_.net资料_编程技术
FCKEditor在Asp.net环境下的配置安装_.net资料_编程技术
LINQ体验系列文章导航_[Asp.Net教程]
序列化和反序列化XML应用程序设置类_[Asp.Net教程]
AJAX技术在PHP开发中的简单应用_php资料_编程技术
将ASP.NET,Control转换为String_[Asp.Net教程]
asp.net,ajax,1.0,hello,world程序_.net资料_编程技术
C、C++输入、输出流[二]
Reading number is top 10 pictures
Sora aoi on twitter4
Sora aoi mirror memorial classics5
NeedWallpaper11
西游日记1
青涩甜美-王祖贤小时候的旧照片曝光
Summer is most suitable for young people to travel in China3
The real super beauty7
From China fortress sora aoi2
Angie Chiu vijara myth2
心有鱼而力不足
Download software ranking
Detective task-the top secret prostitution files
Call Of Duty2
Boxer's Top ten classic battle7
ASP.NET.2.0.XML.高级编程(第3版)
Boxer's Top ten classic battle3
Be there or be square
linux初级教程
apache-tomcat-6.0.33
致我们终将逝去的青春
Tram sex maniac 2 (H) rar bag1




System.DateTime


0001 年 1 月 1 日


9999 年 12 月 31 日


100 毫微秒(0.0000001 秒)


T-SQL DateTime


1753 年 1 月 1 日


9999 年 12 月 31 日


3.33… 毫秒(0.0033333 秒)


T-SQL SmallDateTime


1900 年 1 月 1 日


2079 年 6 月 6 日


1 分钟(60 秒)


CLR DateTime 类型与SQL Server类型相比,前者范围更大、精度更高。因此来自SQL Server的数据用CLR类型表示时,绝不会损失量值或精度。但如果反过来的话,则范围可能会减小,精度可能会降低;SQL Server日期不存在TimeZone概念,而在CLR中支持这个功能。
我们在LINQ to SQL查询使用以当地时间、UTC 或固定时间要自己执行转换。


下面用三个实例说明一下。


16.DateTime.Year

var q =
from o in db.Orders
where o.OrderDate.Value.Year == 1997
select o;

17.DateTime.Month

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

18.DateTime.Day

var q =
from o in db.Orders
where o.OrderDate.Value.Day == 31
select o;


  作者:李永京(YJingLee's Blog)
  出处:http://lyj.cnblogs.com





归海一刀 published in(发表于) 2014/1/30 1:02:36 Edit(编辑)
LINQ体验(11)——LINQ,to,SQL语句之Null语义和String、DateTime方法_[Asp.Net教程]

LINQ体验(11)——LINQ,to,SQL语句之Null语义和String、DateTime方法_[Asp.Net教程]

LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法_[Asp.Net教程]

  在本系列中,主要介绍LINQ to SQL基础的东西,在上一篇中,听取了老赵的意见,为了避免初学者对于LINQ与LINQ to SQL这两个概念的误解,将本系列命名为LINQ体验系列之LINQ to SQL语句,注意一下请转载本系列的朋友相应的修改一下题目。因为LINQ太强大了,它对我们平常使用不同的数据源有着不同的内容,其包括对于SQL Server 数据库的LINQ to SQL;对于XML 文档的LINQ to XML;对于 ADO.NET 数据集的LINQ to DataSet;对于.NET 集合、文件、字符串等的LINQ to Objects。例外也出现了一些对LINQ支持的开源项目,例如LINQ to JSON,LINQ for NHibernate等等。在这个系列中,一些关于LINQ to SQL基础的东西就这么多了,这一篇用一些例子说明一下Null语义和String/DateTime方法。


Null语义


说明:下面第一个例子说明查询ReportsToEmployee为null的雇员。第二个例子使用Nullable.HasValue查询雇员,其结果与第一个例子相同。在第三个例子中,使用Nullable.Value来返回ReportsToEmployee不为null的雇员的ReportsTo的值。


1.Null

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

2.Nullable.HasValue

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

3.Nullable.Value

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

String/Date Functions(字符串/日期方法)


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 };

这个例子使用了+操作符重新组合了顾客的区域。


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(" ")};

这个实例说明ContactName第一个匹配项的索引有一个空格的顾客。


下面两个实例说明ContactName以"Maria"开头,以"Anders"结尾的顾客。接下来的方法看看例子很快就明白了。


5.String.StartsWith(prefix)

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

6.String.EndsWith(suffix)

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

7.String.Substring(start)

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

8.String.Substring(start, length)

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

9.String.ToUpper()

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

10.String.ToLower()

var q =
from c in db.Categories
select c.CategoryName.ToLower();

11.String.Trim()

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

12.String.Insert(pos, str)

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

13.String.Remove(start)

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

14.String.Remove(start, length)

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

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")
};



LINQ to SQL支持以下DateTime方法。但是,SQL Server和CLR的DateTime类型在范围和计时周期精度上不同,如下表。



类型


最小值


最大值


计时周期



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.