「Unity/Csharp/linq」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→like後方一致) |
(→GroupByを使う場合) |
||
行132: | 行132: | ||
==GroupByを使う場合== | ==GroupByを使う場合== | ||
<pre> | <pre> | ||
− | public IEnumerable<IEnumerable< | + | public IEnumerable<IEnumerable<User>> FindAllGroupByIdOrderByCntDesc() |
{ | { | ||
− | List< | + | List<User> users = FindAll(); |
− | return | + | return users |
.GroupBy(value => value.id) | .GroupBy(value => value.id) | ||
.OrderByDescending(value => value.Count()); | .OrderByDescending(value => value.Count()); | ||
} | } | ||
</pre> | </pre> |
2020年5月16日 (土) 13:50時点における版
目次
準備
using System.Linq;
配列データの用意
users
User.cs
[Serializable] public class User { [SerializeField] public int age; [SerializeField] public string name; } List<User> users = new List<User>(); users.Add(new User() { age = 10, name = "taro" }); users.Add(new User() { age = 13, name = "jiro" }); users.Add(new User() { age = 12, name = "saburo" }); users.Add(new User() { age = 15, name = "siro" });
scores
Score.cs [Serializable] public class Score { [SerializeField] public string yyyymmdd; [SerializeField] public int missCnt; [SerializeField] public int finishCnt; } List<Score> scores = new List<Score>(); scores.Add(new Score() { yyyymmdd = "20190316", missCnt = 10, finishCnt = 1 }); scores.Add(new Score() { yyyymmdd = "20190315", missCnt = 13, finishCnt = 2 }); scores.Add(new Score() { yyyymmdd = "20190314", missCnt = 12, finishCnt = 3 }); scores.Add(new Score() { yyyymmdd = "20190313", missCnt = 15, finishCnt = 4 });
条件一行だけ(fetchRow)
public ScoreDay FindRowByYyyymmdd(string yyyymmdd) { List<ScoreDay> scores = FindAll(); return scores.FirstOrDefault(score => score.yyyymmdd == yyyymmdd); } // 検索にひっかからないとnullが帰ってくる ScoreDay score = dao.FindRowByYyyymmdd("201903"); if (score == null) return;
条件複数行(fetchAll)
public IEnumerable<ScoreDay> FindAllByYyyymmdd(string yyyymmdd) { List<ScoreDay> scores = FindAll(); return scores.Where(score => score.yyyymmdd == yyyymmdd); }
条件複数行でソート(fetchAll)
public IOrderedEnumerable <ScoreDay> FindAllByYyyymmdd(string yyyymmdd) { List<ScoreDay> scores = FindAll(); return scores.Where(score => score.yyyymmdd == yyyymmdd).OrderByDescending( value => value.age ); }
順序
// 年下順 IOrderedEnumerable<User> orderedUsers = users.OrderBy(value => value.age); // 年上順 IOrderedEnumerable<User> orderedDescUsers = users.OrderByDescending(value => value.age); foreach (User user in orderedUsers) { Debug.Log(user.name); }
取得件数を絞る
List<Score> scores = new List<Score>(); IEnumerable<Score> tmpScores = scores.Skip(0).Take(100);
指定条件だけに絞る
List<ScoreDay> scores = FindAll(); IEnumerable<Score> tmpScores = scores.Where(score => score.sec > 6); List<Score> scores = tmpScores.ToList();
条件に引っかからなかった時
List<ScoreDay> scores = FindAll(); IEnumerable<Score> tmpScores = scores.Where(score => score.sec > 6); if (!tmpScores.Any()) { return 0; }
IEnumerableをListへ変換
IEnumerable<Score> tmpScores = // 略・・・ List<Score> scores = tmpScores.ToList());
数値の合計
List<Score> scores = FindAll(); return scores.Sum(record => record.missCnt);
like検索
List<Score> scores = FindAll(); IEnumerable<Score> scores = scores.Where(record => record.yyyymmdd.Contains("201903")); // .Contains("/201903/")); // こういうスラッシュはいらない
likeしたあとの合計
IEnumerable<Score> scores = scores.Where(record => record.yyyymmdd.Contains("201903")); Debug.Log("sum=" + scores.Sum(record => record.missCnt));
like前方一致
List<Score> scores = FindAll(); IEnumerable<Score> scores = scores.Where(record => record.yyyymmdd.StartsWith("201903"));
like後方一致
List<Score> scores = FindAll(); IEnumerable<Score> scores = scores.Where(record => record.yyyymmdd.EndsWith("0316"));
GroupByを使う場合
public IEnumerable<IEnumerable<User>> FindAllGroupByIdOrderByCntDesc() { List<User> users = FindAll(); return users .GroupBy(value => value.id) .OrderByDescending(value => value.Count()); }