linqサンプル

[.net]linqサンプル

//linq test code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace LinqTest
{

    class Word
    {
        public string data;
        public Word(string s) { data = s; }
    }

    class Customer
    {
        public string id = "";
        public string name = "";
        public int old = 0;
        public string country = "";
        public Customer(string id, string name, int old, string country)
        {
            this.id =
                id; this.name = name; this.old = old; this.country = country;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //test-data
                Word[] words =
                {
                new Word("b"),
                new Word("k"),
                new Word("ddddd"),
                new Word("fffff"),
                new Word("aaa"),
                new Word("mmm"),
                new Word("e"),
                new Word("ccc"),
                };

                Customer[] customers =
                {
                new Customer("3","aoi-koba",10, "jpn"),
                new Customer("2","eiko-tanaka",20, "usa"),
                new Customer("1","yoko-nakamura",40, "jpn"),
                new Customer("1","yoko-nakamura",33, "usa"),
                new Customer("1","miki-yosimuar",11, "jpn"),
                new Customer("2","mari-hada",22 ,"usa"),
                new Customer("1","eri-nakamura",33 ,"cha"),
                };


                //単一カラムのソート
                foreach (var v in words.OrderBy(x => x.data.Length).ToArray())
                {
                    Console.WriteLine(v.data);
                }
                Console.WriteLine("");


                //マルチカラムのソート
                foreach (var v in customers.OrderBy(x => x.id).ThenBy(n => n.name).ThenBy(k
                => k.old).ToArray())
                {
                    Console.WriteLine(v.id + ":" + v.name + ":" + v.old);
                }

                //マルチカラムのソート
                Console.WriteLine("");
                var vvv = from x in customers orderby x.id, x.old select x;
                foreach (var cc in vvv)
                {
                    Console.WriteLine(cc.id + ":" + cc.name + ":" + cc.old);
                }

                //条件セレクト、一部の項目のみセレクト
                Console.WriteLine("");
                var vv = from x in customers where x.id == "3" select new { x.id, x.name };
                foreach (var cc in vv)
                {
                    Console.WriteLine(cc.id + ":" + cc.name);
                }

                //グループ化
                var vvvv = from x in customers group x by x.country;
                foreach (IGrouping<string, Customer> r in vvvv)
                {
                    Console.WriteLine(r.Key);
                    foreach (var item in r)
                    {
                        Console.WriteLine(" " + item.id + ":" + item.name + ":" + item.old);
                    }
                }

                //linq-xml saimple-1
                Console.WriteLine(" ");
                string xmldoc =
                @"<?xml version='1.0'?>
                    <names xmlns='http://www.piedey.co.jp/example/linqtoxml200811'>
                      <name id='X'>Xenogears</name>
                      <name id='M'>Mystic Quest</name>
                      <name id='L'>LEGEND OF MANA</name>
                    </names>
                    ";

                var doc = XElement.Parse(xmldoc);
                XNamespace ex = "http://www.piedey.co.jp/example/linqtoxml200811";
                var qxml = from n in doc.Descendants(ex + "name")
                           where
                               n.Attribute("id").Value == "M"
                           select n;
                foreach (var elem in qxml)
                {
                    Console.WriteLine(elem.Value);
                }


                //linq-xml saimple-2
                var doc2 = XElement.Load("test.xml");
                var qxml2 = from n2 in doc2.Elements("user") select n2;

                foreach (var elem in qxml2)
                {
                    XElement xelem = elem.Element("birthday");
                    string year = xelem.Element("year").Value;
                    string month = xelem.Element("month").Value;
                    string day = xelem.Element("day").Value;
                    string name = elem.Element("name").Value;
                    string old = elem.Element("old").Value;

                    Console.WriteLine(string.Format("{0} {1} {2} {3} {4}", name, old, year,
                    month, day));
                }

                Console.WriteLine("");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.ReadLine();
            }
        }
    }
}