在C#中对相关项的集合进行排序

时间:2011-10-29 10:48:07

标签: c# linq

我有一个List,其中包含类型A

的数据
Class A
{
int Year
int CountryID
string City
decimal SalesAmount
}

我的要求是将此列表绑定到一个网格,该网格按年份和国家分组显示项目,并按销售额desc排序

我通过订购Year和CountryID读取值来实现我的一半要求,但在应用排序时面临问题

以下是我用来分组的方法。 在后面的代码我通过在Year和CountryId上排序读取值,在aspx页面中我调整rowspan以将它们显示为分组

from t in listA
order by t.Year,t.CountryID

那么,有没有一种方法可以通过考虑同年和同一国家的条件或我的方法是错误的方式对salesamount进行排序?

向帖子添加更多信息:

     List<A> list = new List<A>() 
{
    new A() { Year = 2001, CountryID = 10,State="A", SalesAmount = 322 },
    new A() { Year = 2011, CountryID = 1,State="A", SalesAmount = 23 },
    new A() { Year = 1983, CountryID = 12,State="A", SalesAmount = 6 },
    new A() { Year = 2011, CountryID = 1,State="B", SalesAmount = 211 },
    new A() { Year = 2011, CountryID = 1,State="C", SalesAmount = 32 },
    new A() { Year = 2001, CountryID = 10,State="B", SalesAmount = 65 },
    new A() { Year = 1983, CountryID = 12,State="C", SalesAmount = 22 },
    new A() { Year = 2001, CountryID = 10,State="C", SalesAmount = 1 },
};

现在我需要在网格上显示这些数据 Data on Grid

2 个答案:

答案 0 :(得分:2)

也许我误解了,但我想你想使用OrderBy()ThenBy() IEnumerable<T> extension methods

类似的东西:

List<A> list = new List<A>() 
{
    new A() { Year = 2001, CountryID = 10, SalesAmount = 322 },
    new A() { Year = 2011, CountryID = 1, SalesAmount = 23 },
    new A() { Year = 1983, CountryID = 12, SalesAmount = 6 },
    new A() { Year = 2011, CountryID = 1, SalesAmount = 211 },
    new A() { Year = 2001, CountryID = 1, SalesAmount = 32 },
    new A() { Year = 2003, CountryID = 10, SalesAmount = 65 },
    new A() { Year = 2050, CountryID = 12, SalesAmount = 22 },
    new A() { Year = 1974, CountryID = 10, SalesAmount = 1 },
};

var sortedList = list
    .OrderBy(a => a.Year)
    .ThenBy(a => a.CountryID)
    .ThenBy(a => a.SalesAmount);

答案 1 :(得分:0)

编辑:

考虑到并根据您的更新,我的原始查询需要调整为这样的内容:

var groupedItems =
    from i in list
    orderby i.Year
    group i by new { i.Year, i.CountryID } into g     
    select new { Key = g.Key, A = g };

<小时/> 我不完全清楚你想要达到的目标,我可以建议:

var groupedItems = 
    from i in list
    orderby i.Year, i.CountryID
    group i by i.SalesAmount into g         
    select new { SalesAmount = g.Key, A = g };

所以,对于像这样的集合:

List<A> list = new List<A>() 
{
    new A() { Year = 2001, CountryID = 10, SalesAmount = 322 },
    new A() { Year = 2011, CountryID = 1, SalesAmount = 23 },
    new A() { Year = 1983, CountryID = 12, SalesAmount = 6 },
    new A() { Year = 2011, CountryID = 1, SalesAmount = 322 },
    new A() { Year = 2001, CountryID = 1, SalesAmount = 322 },
    new A() { Year = 2003, CountryID = 10, SalesAmount = 6 },
    new A() { Year = 2050, CountryID = 12, SalesAmount = 23 },
    new A() { Year = 1974, CountryID = 10, SalesAmount = 23 },
};

我们可以将结果表示如下:

Group1 (23)
  - A { 1974, 10, 23 }
  - A { 2011, 1, 23 }
  - A { 2050, 12, 23 }

Group2 (6)
  - A { 1983, 12, 6 }
  - A { 2003, 10, 6 }

Group3 (322)
  - A { 2001, 1, 322 }
  - A { 2001, 10, 322 }
  - A { 2011, 1, 322 }

我想你可能会想要订购这些组,这将是微不足道的。