| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

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

案内メニュー