==C# LINQ使用例==
[[C#][C# サンプルコード]]
*[http://iss.ndl.go.jp/information/api/ 国立国会図書館API]
using System;
{
var p = new Program();
if (args.Length < < 2)
{
Console.WriteLine("must 2 arguments.");
else if (option == "-k")
{
var param = new List<<KeyValuePair<<string, string>>>>(); for (int i=1; i<<args.Length; i++)
{
string[] kv = args[i].Split('=');
param.Add(new KeyValuePair<<string, string>>(kv[0], kv?[1]));
}
xml = p.GetXml(param);
else if (option == "-t")
{
var param = new List<<KeyValuePair<<string, string>>>>(); param.Add(new KeyValuePair<<string, string>>("title", "Python"));
xml = p.GetXml(param);
}
}
private static void LinqTest(List<<Book> > books)
{
}
/// <<summary>>
/// メソッド構文
/// <</summary>> /// <<param name="books"><></param>> private static void MethodExp(List<<Book> > books)
{
var result = books
.Where( book => > book.Title.IndexOf("計算") > > 0 ) .OrderByDescending( book=> > book.Title) .Select( book => > book.Title)
;
}
/// <<summary>>
/// クエリ構文
/// <</summary>> /// <<param name="books"><></param>> private static void QueryExp(List<<Book> > books)
{
var result = from book in books
where book.Title.IndexOf("計算") > > 0
orderby book.Title descending
select book.Title
// 遅延評価
// books.ForEach(book => > book.Title = book.Title.Replace("Python", "HOGE"));
foreach (string title in result)
}
private static List<<Book> > ParseXml(string xml)
{
XNamespace xmlns = "http://www.loc.gov/zing/srw/";
;
var books = new List<<Book>>();
foreach (XElement record in records)
{
}
public string GetUrl(List<<KeyValuePair<<string, string>> >> param)
{
string url = "http://iss.ndl.go.jp/api/sru?operation=searchRetrieve&recordPacking=xml&query={0}";
string qp = string.Join(" OR ",
param.Select(kvp => > WebUtility.UrlEncode(String.Format("{0}=\"{1}\"", kvp.Key, kvp.Value))));
return string.Format(url, qp);
}
public string GetXml(List<<KeyValuePair<<string, string>> >> param)
{
string url = GetUrl(param);