「Unity/Csharp/linq」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→IEnumerableをListへ変換) |
(→取得件数を絞る) |
||
行43: | 行43: | ||
List<Score> scores = new List<Score>(); | List<Score> scores = new List<Score>(); | ||
IEnumerable<Score> tmpScores = scores.Skip(0).Take(100); | IEnumerable<Score> tmpScores = scores.Skip(0).Take(100); | ||
+ | |||
+ | ==指定条件だけに絞る== | ||
+ | IEnumerable<Score> tmpScores = scores.Where(score => score.sec > 6); | ||
+ | List<Score> scores = tmpScores.ToList(); | ||
==IEnumerableをListへ変換== | ==IEnumerableをListへ変換== | ||
IEnumerable<Score> tmpScores = // 略・・・ | IEnumerable<Score> tmpScores = // 略・・・ | ||
List<Score> scores = tmpScores.ToList()); | List<Score> scores = tmpScores.ToList()); |
2019年3月9日 (土) 17:07時点における版
準備
using System.Linq;
条件一行だけ(fetchRow)
public ScoreDay FindRowByYyyymmdd(string yyyymmdd) { List<ScoreDay> scores = FindAll(); return scores.FirstOrDefault(score => score.yyyymmdd == yyyymmdd); }
条件複数行(fetchAll)
public IEnumerable<ScoreDay> FindAllByYyyymmdd(string yyyymmdd) { List<ScoreDay> scores = FindAll(); return scores.Where(score => score.yyyymmdd == yyyymmdd); }
順序
User[] users = new User[] { new User() { age = 10, name = "taro" }, new User() { age = 13, name = "jiro" }, new User() { age = 12, name = "saburo" }, new User() { age = 15, name = "siro" }, }; // 年下順 IOrderedEnumerable<Parameter> orderedUsers = users.OrderBy( value => value.age ); // 年上順 IOrderedEnumerable<Parameter> orderedUsers = users.OrderByDescending( value => value.age );
取得件数を絞る
List<Score> scores = new List<Score>(); IEnumerable<Score> tmpScores = scores.Skip(0).Take(100);
指定条件だけに絞る
IEnumerable<Score> tmpScores = scores.Where(score => score.sec > 6); List<Score> scores = tmpScores.ToList();
IEnumerableをListへ変換
IEnumerable<Score> tmpScores = // 略・・・ List<Score> scores = tmpScores.ToList());