「C Sharp LINQ使用例」の版間の差分
ナビゲーションに移動
検索に移動
1行目: | 1行目: | ||
− | ==C# LINQ使用例== | + | ==[[C# LINQ使用例]]== |
[[C Sharp]] | [[C Sharp サンプルコード]] | | [[C Sharp]] | [[C Sharp サンプルコード]] | | ||
*[http://iss.ndl.go.jp/information/api/ 国立国会図書館API] | *[http://iss.ndl.go.jp/information/api/ 国立国会図書館API] | ||
7行目: | 7行目: | ||
using System.Linq; | using System.Linq; | ||
using System.Net; | using System.Net; | ||
− | using System. | + | using System.[[R]]eflection; |
using System.Text; | using System.Text; | ||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||
45行目: | 45行目: | ||
{ | { | ||
var param = new List<KeyValuePair<string, string>>(); | var param = new List<KeyValuePair<string, string>>(); | ||
− | param.Add(new KeyValuePair<string, string>("title", "Python")); | + | param.Add(new KeyValuePair<string, string>("title", "[[Python]]")); |
xml = p.GetXml(param); | xml = p.GetXml(param); | ||
} | } | ||
93行目: | 93行目: | ||
// 遅延評価 | // 遅延評価 | ||
− | // books.ForEach(book => book.Title = book.Title.Replace("Python", "HOGE")); | + | // books.ForEach(book => book.Title = book.Title.Replace("[[Python]]", "HOGE")); |
foreach (string title in result) | foreach (string title in result) | ||
107行目: | 107行目: | ||
XNamespace xmlns3 = "http://purl.org/dc/elements/1.1/"; | XNamespace xmlns3 = "http://purl.org/dc/elements/1.1/"; | ||
− | var root = XElement.Load(new | + | var root = XElement.Load(new XmlText[[R]]eader(new String[[R]]eader(xml))); |
var records = from record in root.Elements(xmlns + "records") | var records = from record in root.Elements(xmlns + "records") | ||
160行目: | 160行目: | ||
public string GetUrl(List<KeyValuePair<string, string>> param) | public string GetUrl(List<KeyValuePair<string, string>> param) | ||
{ | { | ||
− | string url = "http://iss.ndl.go.jp/api/sru?operation= | + | string url = "http://iss.ndl.go.jp/api/sru?operation=search[[R]]etrieve&recordPacking=xml&query={0}"; |
− | string qp = string.Join(" | + | string qp = string.Join(" O[[R]] ", |
param.Select(kvp => WebUtility.UrlEncode(String.Format("{0}=\"{1}\"", kvp.Key, kvp.Value)))); | param.Select(kvp => WebUtility.UrlEncode(String.Format("{0}=\"{1}\"", kvp.Key, kvp.Value)))); | ||
return string.Format(url, qp); | return string.Format(url, qp); | ||
172行目: | 172行目: | ||
using (var client = new WebClient()) | using (var client = new WebClient()) | ||
{ | { | ||
− | using (var reader = new | + | using (var reader = new Stream[[R]]eader(client.Open[[R]]ead(url))) |
{ | { | ||
− | string xml = reader. | + | string xml = reader.[[R]]eadToEnd(); |
Console.WriteLine(xml); | Console.WriteLine(xml); | ||
return xml; | return xml; |
2020年2月16日 (日) 04:22時点における最新版
C# LINQ使用例
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; namespace Book { class Program { static void Main(string[] args) { var p = new Program(); if (args.Length < 2) { Console.WriteLine("must 2 arguments."); return; } string option = args[0]; string xml = null; if (option == "-f") { xml = File.ReadAllText(args[1],Encoding.UTF8); } 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); } var books = ParseXml(xml); LinqTest(books); } private static void LinqTest(List<Book> books) { // QueryExp(books); MethodExp(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) ; foreach (string title in result) { Console.WriteLine(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) { Console.WriteLine(title); } } private static List<Book> ParseXml(string xml) { XNamespace xmlns = "http://www.loc.gov/zing/srw/"; XNamespace xmlns2 = "info:srw/schema/1/dc-v1.1"; XNamespace xmlns3 = "http://purl.org/dc/elements/1.1/"; var root = XElement.Load(new XmlTextReader(new StringReader(xml))); var records = from record in root.Elements(xmlns + "records") .Elements(xmlns + "record") .Elements(xmlns + "recordData") .Elements(xmlns2 + "dc") select record ; var books = new List<Book>(); foreach (XElement record in records) { var book = new Book(); books.Add(book); foreach(PropertyInfo field in book.GetType().GetProperties()) { foreach (var elms in record.Elements(xmlns3 + field.Name.ToLower())) { field.SetMethod?.Invoke(book, new object[] { elms.Value }); break; } } } return books; } class Book { public string Title { get; set; } = ""; public string Creator { get; set; } = ""; public string Subject { get; set; } = ""; public string Publisher { get; set; } = ""; public string Language { get; set; } = ""; public override string ToString() { var buf = new StringBuilder(); buf.Append($"{this.GetType().Name}{{"); foreach(PropertyInfo p in this.GetType().GetProperties()) { buf.Append($"{p.Name}={p.GetValue(this)},"); } buf.Append($"}}"); return buf.ToString(); } } 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); using (var client = new WebClient()) { using (var reader = new StreamReader(client.OpenRead(url))) { string xml = reader.ReadToEnd(); Console.WriteLine(xml); return xml; } } } } }
© 2006 矢木浩人