如何通过两个不同类型的字段来订购linq查询结果

时间:2012-08-03 14:33:29

标签: c# winforms linq-to-sql sql-order-by bindingsource

  

可能重复:
  Multiple “order by” in LINQ

我想知道如何通过两种不同的类型订购,如下一个查询:

            var qry = (from m in dc.Orders
                   select new { m.OrderDate, m.OrderID }).OrderBy(o=>o.OrderDate+o.OrderID).ToList();
        bindingSource1.DataSource = qry;

OrdrerDate是日期类型,OrderID是int类型。

4 个答案:

答案 0 :(得分:6)

您正在寻找.ThenBy()

.OrderBy(o => o.OrderDate).ThenBy(o => o.OrderID).

答案 1 :(得分:1)

我会说:

 var qry = (from m in dc.Orders
                   select new { m.OrderDate, m.OrderID }).OrderBy(o=>o.OrderDate).ThenBy(o.OrderID).ToList();

答案 2 :(得分:1)

试试这个

var qry = (from m in dc.Orders 
               select new { m.OrderDate, m.OrderID }).OrderBy(o=>o.OrderDate).ThenBy(o.OrderID).ToList();

答案 3 :(得分:1)

您可以使用ThenBy或者您可以编写此Linq查询:

var qry = (from m in dc.Orders
            orderby m.OrderDate, m.OrderID
    select new {m.OrderDate, m.OrderID}).ToList

        bindingSource1.DataSource = qry;