LoginSignup
17
20

More than 5 years have passed since last update.

LINQのメソッド構文

Posted at
キーワード クエリメソッド
select Select()
where Where()
orderby OrderBy() OrderByDescending()
join Join()
group GroupBy()

その他のメソッド

メソッド 説明
All(条件) すべての要素が条件を満たしていれば true を返す
Any(条件) いずれかの要素が条件を満たしていれば true を返す
Average() 平均値を返す
Contains(引数) 引数に指定されたオブジェクトが含まれていれば true を返す
Count() 要素数を返す
First() 最初の要素を返す
Last() 最後の要素を返す
Max() 最大値を返す
Min() 最小値を返す
Sum() 合計を返す
Where()とSelect()
int[] nums = { 1, -2, 3, 0, -4, 5 };

var res = nums.Where (n => n > 0).Select (r => r);

foreach (int i in res) {
    Console.WriteLine(i);
}
OrderByDescending()
int[] nums = { 1, -2, 3, 0, -4, 5 };

var res = nums.Where(n => n > 0).OrderByDescending(j => j);

foreach (int i in res) {
    Console.WriteLine(i);
}
GroupBy()
string[] websites = {
    "A.com", "B.net", "C.net", "D.com", "E.org",
    "F.org", "G.tv", "H.net", "I.tv"
};

var webAddrs = websites.Where (w => w.LastIndexOf (".") != -1).
                GroupBy (x => x.Substring (x.LastIndexOf (".")));

foreach (var sites in webAddrs) {
    Console.WriteLine(sites.Key);
    foreach (var site in sites) {
        Console.WriteLine(" " + site);
    }
}
intoによる継続のメソッド構文
string[] websites = {
    "A.com", "B.net", "C.net", "D.com", "E.org",
    "F.org", "G.tv", "H.net", "I.tv"
};

// var webAddrs = from addr in websites
//                where addr.LastIndexOf (".") != -1
//                group addr by addr.Substring (addr.LastIndexOf ("."))
//                into ws
//                where ws.Count () > 2
//                select ws;
var webAddrs = websites.Where (w => w.LastIndexOf (".") != -1).
                GroupBy (x => x.Substring (x.LastIndexOf ("."))).
                Where (ws => ws.Count () > 2).
                Select (r => r);

Console.WriteLine ("Top-level domains with more than 2 members.\n");
foreach (var sites in webAddrs) {
    Console.WriteLine (sites.Key);
    foreach (var site in sites) {
        Console.WriteLine (" " + site);
    }
    Console.WriteLine ();
}
Join()
Item[] items = {
    new Item ("ItemA", 1111),
    new Item ("ItemB", 2222),
    new Item ("ItemC", 3333),
    new Item ("ItemD", 4444)
};

InStockStatus[] statusList = {
    new InStockStatus (1111, true),
    new InStockStatus (2222, false),
    new InStockStatus (3333, true),
    new InStockStatus (4444, true)
};

var inStockList = items.Join(statusList,
                    k1 => k1.ItemNumber,
                    k2 => k2.ItemNumber,
                    (k1, k2) => new { k1.Name, k2.InStock } );

Console.WriteLine ("Item\tAvailable\n");
foreach (var t in inStockList) {
    Console.WriteLine ("{0}\t{1}", t.Name, t.InStock);
}
グループ結合のメソッド構文
string[] travelTypes = {
    "Air",
    "Sea",
    "Land",
};

Transport[] transports = {
    new Transport ("Bicycle", "Land"),
    new Transport ("Balloon", "Air"),
    new Transport ("Boat", "Sea"),
    new Transport ("Jet", "Air"),
    new Transport ("Canoe", "Sea"),
    new Transport ("Biplane", "Air"),
    new Transport ("Car", "Land"),
    new Transport ("Cargo Ship", "Sea"),
    new Transport ("Train", "Land")
};

// var byHow = from how in travelTypes
//             join trans in transports
//             on how equals trans.How
//             into lst
//             select new { How = how, Tlist = lst };
var byHow = travelTypes.Join (transports, k1 => k1, k2 => k2.How, (k1, k2) => new { How = k1, Name = k2.Name }).
            GroupBy (x => x.How).
            Select (r => r);

foreach (var t in byHow) {
//  Console.WriteLine ("{0} transportation includes:", t.How);
//  foreach (var m in t.Tlist) {
    Console.WriteLine ("{0} transportation includes:", t.Key);
    foreach (var m in t) {
        Console.WriteLine (" " + m.Name);
    }
    Console.WriteLine ();
}
17
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
20