0

我在大学项目的终点线上,我有点坚持完成一个查询。工作查询如下所示:

var Report = from query in Document.Descendants("order")
                         group query by query.Element("seller").Value
                            into qGroup
                            select new Orders
                            {
                                Seller = qGroup.Key,
                                Quantity = qGroup.Sum(p => int.Parse(p.Element("quantity").Value)).ToString()
                            };

如果您能告诉我如何按给定的“数量”按降序排列结果,我将不胜感激。

谢谢!

4

3 回答 3

3
var Report = (from query in Document.Descendants("order")
              group query by query.Element("seller").Value into qGroup
              select new Orders
              {
                  Seller = qGroup.Key,
                  Quantity = qGroup.Sum(p => int.Parse(p.Element("quantity").Value)).ToString()
              })
              .OrderByDescending(order => order.Quantity);
于 2012-04-16T16:30:04.943 回答
0

请这样做:

var Report = (from query in Document.Descendants("order")
                            group query by query.Element("seller").Value
                            into qGroup
                            select new Orders
                            {
                                Seller = qGroup.Key,
                                Quantity = qGroup.Sum(p => int.Parse(p.Element("quantity").Value)).ToString()
                            }).OrderByDescending(x => x.Quantity);

呼!!!!

(对LINQ Orderby 降序查询表示敬意

于 2012-04-16T16:33:41.857 回答
0

Skippy 和 Andrei 的答案有效,但你也可以写成

var Report = from query in Document.Descendants("order")
             group query by query.Element("seller").Value
             into qGroup
             let qty = qGroup.Sum(p =>int.Parse(p.Element("quantity").Value)).ToString()
             orderby qty descending
             select new Orders
             {
                 Seller = qGroup.Key,
                 Quantity = qty
             };

如果您宁愿将其全部保存在一种语法中。

于 2012-04-16T16:50:34.607 回答