Linqを使ってみた。
using System;
using System.Linq;
class Program
{
static void PrintData(Object data)
{
Type type = data.GetType();
if (data is System.Collections.IEnumerable)
{
PrintList(data as System.Collections.IEnumerable);
}
}
static void PrintList(System.Collections.IEnumerable dataList)
{
foreach (var v in dataList)
{
if (v is System.Collections.IEnumerable)
{
PrintList(v as System.Collections.IEnumerable);
}
else
{
Console.WriteLine(v.ToString());
}
}
}
static void Main(string[] args)
{
var brandList = new[]
{
new {Group=10, Name="AGATH PRADA" , Yomi="アガタプラダ"},
new {Group=20, Name="agnes b." , Yomi="アニエス・ベー"},
new {Group=30, Name="Agronatura" , Yomi="アグロナチュラ"},
new {Group=10, Name="ANNA MOLINARI", Yomi="アンナ・モリナーリ"},
new {Group=20, Name="ANNA SUI" , Yomi="アナ・スイ"},
new {Group=30, Name="Anne Klein" , Yomi="アン・クライン"},
new {Group=10, Name="an ten na" , Yomi="アンテナ"},
new {Group=20, Name="ANTIK DENIM" , Yomi="アンティックデニム"},
};
Console.WriteLine("[brandList]");
PrintData(brandList);
Console.WriteLine();
Console.WriteLine("> from p in brandList where p.Name.Equals(\"ANNA\") select p");
var annaList = from p in brandList where p.Name.Equals("ANNA") select p;
PrintData(annaList);
Console.WriteLine();
Console.WriteLine("> from p in brandList where (p.Name.Length < 10) select p");
var shortList = from p in brandList where (p.Name.Length < 10) select p;
PrintData(shortList);
Console.WriteLine();
Console.WriteLine("> from p in brandList orderby p.Group descending group p by p.Group");
var groupList = from p in brandList orderby p.Group descending group p by p.Group;
PrintData(groupList);
Console.WriteLine();
}
}
結果は以下。
[brandList]
{ Group = 10, Name = AGATH PRADA, Yomi = アガタプラダ }
{ Group = 20, Name = agnes b., Yomi = アニエス・ベー }
{ Group = 30, Name = Agronatura, Yomi = アグロナチュラ }
{ Group = 10, Name = ANNA MOLINARI, Yomi = アンナ・モリナーリ }
{ Group = 20, Name = ANNA SUI, Yomi = アナ・スイ }
{ Group = 30, Name = Anne Klein, Yomi = アン・クライン }
{ Group = 10, Name = an ten na, Yomi = アンテナ }
{ Group = 20, Name = ANTIK DENIM, Yomi = アンティックデニム }
> from p in brandList where p.Name.Equals("ANNA") select p
> from p in brandList where (p.Name.Length < 10) select p
{ Group = 20, Name = agnes b., Yomi = アニエス・ベー }
{ Group = 20, Name = ANNA SUI, Yomi = アナ・スイ }
{ Group = 10, Name = an ten na, Yomi = アンテナ }
> from p in brandList orderby p.Group descending group p by p.Group
{ Group = 30, Name = Agronatura, Yomi = アグロナチュラ }
{ Group = 30, Name = Anne Klein, Yomi = アン・クライン }
{ Group = 20, Name = agnes b., Yomi = アニエス・ベー }
{ Group = 20, Name = ANNA SUI, Yomi = アナ・スイ }
{ Group = 20, Name = ANTIK DENIM, Yomi = アンティックデニム }
{ Group = 10, Name = AGATH PRADA, Yomi = アガタプラダ }
{ Group = 10, Name = ANNA MOLINARI, Yomi = アンナ・モリナーリ }
{ Group = 10, Name = an ten na, Yomi = アンテナ }
参考:「LINQ: .NET 統合言語クエリ」
追記:表示部分が苦しいな。もっと何とかならないものか・・。