惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
I
InfoQ
B
Blog RSS Feed
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
Vercel News
Vercel News
Recent Announcements
Recent Announcements
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Palo Alto Networks Blog
S
Schneier on Security
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
Latest news
Latest news
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
T
The Exploit Database - CXSecurity.com
T
Tor Project blog
A
Arctic Wolf
博客园 - 叶小钗
K
Kaspersky official blog
U
Unit 42
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
H
Help Net Security
V2EX - 技术
V2EX - 技术
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
Cloudbric
Cloudbric
Hacker News: Ask HN
Hacker News: Ask HN
博客园 - 三生石上(FineUI控件)
C
Cisco Blogs
D
DataBreaches.Net
Project Zero
Project Zero
The Cloudflare Blog
罗磊的独立博客
WordPress大学
WordPress大学
Y
Y Combinator Blog
Attack and Defense Labs
Attack and Defense Labs
腾讯CDC
V
V2EX
F
Full Disclosure
H
Heimdal Security Blog

博客园 - baixve

战略机遇期来了<-->抓住 知识管理读书笔记(2) 知识管理读书笔记(1) 暂别…… [转MSDN文档]SQL Server 2008 Analysis Services 入门教案 聚集索引和非聚集索引的区别 告别一年后我有回来了!--何为Windows DNA LINQ 集成到应用程序中需考虑的一些问题 构建 WPF 数独游戏第一部分:WPF 和 XAML 简介 2008,I'm Ready! 参加微软新技术预览大会 为我的BePareShop添加Ajax功能 只为更新 [转]C#3.0入门系列(十二)-Lambda表达式中Lifting [转]C#3.0入门系列(十)-之Join操作 [转]C#3.0入门系列(十一)-之In, Like操作 - baixve - 博客园 [转]C#3.0入门系列(九)-之GroupBy操作 [转]C#3.0入门系列(八)-之GroupBy操作 [转]C#3.0入门系列(七)--之OR工具介绍
[转]C#3.0入门系列(六)-之OrderBy操作
baixve · 2007-10-24 · via 博客园 - baixve

本节讲orderby操作.我突然在想这么一个问题,读者会T-SQL吗?要是不知道,那我写的是不是太简单了呢?做个调查哦,不知道的举手.
OrderBy操作
简单的,按雇用日期排序,默认为升序

            var q =
                from e 
in db.Employees
                orderby e.HireDate
                select e;

带where条件的,shipcity为london的。

            var q =
                from o 
in db.Orders
                where o.ShipCity 
== "London"
                orderby o.Freight
                select o;

            var q 
=
                from o 
in db.Orders
                orderby o.Freight
                where o.ShipCity 
== "London"
                select o;

在这里where和orderby的顺序并不重要。而在T-SQL中,where和orderby有严格的位置限制。
OrderByDescending的,按价格降序。

            var q =
                from p 
in db.Products
                orderby p.UnitPrice descending
                select p;

ThenBy的和ThenByDescending,也就是按多个列进行排序,第一个列子是先按city,city相同的再按contactname排序,第二个例子中,第二序列为降序。

ThenBy:
            var q 
=
                from c 
in db.Customers
                orderby c.City, c.ContactName
                select c;

ThenByDescending:
            var q 

=
                from o 
in db.Orders
                where o.EmployeeID 
== 1
                orderby o.ShipCountry, o.Freight descending
                select o;

对这两个句子解释下。

对于ThenBy操作,其级连形式为:
       var q 
= db.Customers.OrderBy(c => c.City).ThenBy(c => c.ContactName).ToList();
因为T
-SQL中,并没有ThenBy语句,其依然翻译为OrderBy
所以,也可以用下面语句来表达
var q 
= db.Customers.OrderBy(c => c.ContactName).OrderBy(c => c.City).ToList();
所要注意的是,是两个orderby的顺序,多个orderby操作时,级连方式是按逆序.即先按city排时,city要放在最后。

对于降序的,用相应的降序操作符替换即刻。

        var q = db.Customers.OrderByDescending(c => c.City).ThenByDescending(c => c.ContactName).ToList();

需要说明的是,orderby操作,不支持按type排序,也不支持匿名类。
比如  var q = db.Customers.OrderBy(c => c).ToList();和
var q = db.Customers.OrderBy(c => new {c.City,c.ContactName}).ToList();

会被抛出异常。但是,既然提了,大家在这个问题就不会犯错误,常见的错误是前面的操作有匿名类,再跟orderby时,比较的是类别。比如

var q = db.Customers.Select(c => new { c.City, c.Address }).OrderBy(c => c).ToList();

如果你想使用OrderBy(c => c),其前提条件是,前面步骤中,所产生的对象的类别必须为C#语言的基本类型。比如
var q = db.Customers.Select(c=>c.City).OrderBy(c => c).ToList();
city为string类型。

还有一点需要说明的时,linq和dlinq在orderby操作中,稍微有一点区别。linq支持按type排序,但是,需要你自己去实现IComparable接口。

比如语句:var q = db.Customers.ToList().OrderBy(c => c).ToList();
第一个ToList()会把数据库中所有数据取出,放到内存中,以后所有的操作全部是对内存操作。后面的所有操作均为linq操作,不是dlinq。(这一点,我前面的文章中讲过)如果,你想用按客户进行排序,你必须在Customer类中,实现IComparable接口

其Customer类,必须从IComparable继承,代码如下,
  public partial class Customers : System.Data.Linq.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged,IComparable
Icomparable接口的具体实现如下:

        #region IComparable Memberspublic int CompareTo(object obj)
        {
            
return this._CustomerID.CompareTo(((Customers)obj).CustomerID);
            
//throw new Exception("The method or operation is not implemented.");
        }#endregion

Orderby操作,会自动调用该接口的方法,实现按类别排序。如果,你的映射文件中没有实现该接口,系统会抛出异常。
你也可以使用generic,如下,
    public partial class Customers : System.Data.Linq.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged,IComparable<Customers>

        #region IComparable<Customers> Memberspublic int CompareTo(Customers other)
        {
            
return this.CustomerID.CompareTo(other.CustomerID);
            
//throw new Exception("The method or operation is not implemented.");
        }#endregion

好处就是你无须把object强制转化为customer类。
我们再来定义个,先按订单号排序,相同订单按产品号排序的。
    public partial class OrderDetails : System.Data.Linq.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged,IComparable<OrderDetails>

        #region IComparable<OrderDetails> Memberspublic int CompareTo(OrderDetails other)
        {
            
int k = this._OrderID - other.OrderID;
            
if (k == 0)
            {
                k 
= this._ProductID - other.ProductID;
            }
            
return k;
            
//throw new Exception("The method or operation is not implemented.");
        }

好了,更多的功能,等待大家自己去实现。下次讲Groupby操作。