ASP.NET MVC LINQ to Entities
目次
LINQ
統合言語クエリー
Language Integrated Query
データベース、データセット、エンティティ、XML、配列、オブジェクト等、
プログラム内のデータに対する統一されたアクセス方法
LINQ to Entities
EntityDataModelに対するLINQ
実例
※モデル、コンテキストを用意しておく(ASP.NETMVC流れ参照)
private ArticleContext _db = new ArticleContext();
public ActionResult Search(string keyword, bool? released)
{
from句
select句
select new ArticleView
{
Title = b.Title.Substring(0, 10),
ViewCount = (int)Math.Ceiling(b.ViewCount/ 1000.0),
Released = (b.Released ? “公開中” : “公開予定”)
};
			別クラス(Model/ArticleView.cs)
			public class ArticleView
			{
			  public string Title { get; set; }
			  public int ViewCount { get; set; }
			  public string Released { get; set; }
			}
			//匿名クラスを利用(「匿名クラス」参照)
			var articles = from b in _db.Article
			  select new
			  {
			    b.Title.Substring(0, 10),
			    b.ViewCount,
			    b.Released
			  };
		
.Select(b => new ArticleView
{
Title = b.Title.Substring(0, 10),
ViewCount = (int)Math.Ceiling(b.ViewCount/ 1000.0),
Released = (b.Released ? “公開中” : “公開予定”)
});
			別クラス(Model/ArticleView.cs)
			public class ArticleView
			{
			  public string Title { get; set; }
			  public int ViewCount { get; set; }
			  public string Released { get; set; }
			}
		
where句
where a.Title == "title1"
selectb;
.Where(a => a.Title == "title1");
.Select(a=>a.Title);
orderby句
orderby b.Published ascending, b.Title descending
select b;
.OrderBy(b => b.Published)
.ThenByDescending(b => b.Title);
			var articles = _db.Articles
			  .OrderByDescending(b => b.Published)
			  .ThenBy(b => b.Title);
			第一キー
			OrderBy(昇順)/OrderByDescending(降順)
			第二キー以降
			ThenBy(昇順)/ThenDescending(降順)
		
contain()
//keywordを含むデータを抽出
var articles = from b in _db.Article
where b.Title.Contains(keyword) select b;
}
articles = articles.Where(a => a.Title.Contains(keyword));
}
==
//true/falseであるデータを抽出
var articles = from b in _db.Article
where b.release == true select b;
};
articles = articles.Where(a => a.Released);
articles = articles.Where(a => a.Released == true);
};
Distinct()
Skip()/Take()
var articles = (from a in _db.Article select a.Title).Skip(5);
//先頭から5レコードのみ取得する
var articles = (from a in _db.Article select a.Title).Take(5);
//先頭から5レコード飛ばし、6レコード目から5レコードのみ取得する
var articles = (from a in _db.Article select a.Title).Skip(5).Take(5);
First()/FirstOrDefault()
var articles = (from a in _db.Article select a.Title).First();
//先頭レコードのみ取得する。該当データ無しの場合はデフォルト値を取得する
var articles = (from a in _db.Article select a.Title).FirstOrDefault();
group by句
			※View
			@model IEnumerable<IGrouping<string , ASPNET_MVC_DB.Models.Article>>
			<body>
			  //elmGrpにはカテゴリーのグループ結果が入る
			  @foreach (var elmGrp in Model)
			  {
			    @elmGrp.Key<br/>
			    //elmにはグループ内の要素が入る
			    foreach (var elm in elmGrp) {
			      @elm.Title<br/>
			    }
			    <br/>
			  }
			</body>
			⇒結果:
			CategoryA
			Title1
			Title2
			CategoryB
			Title3
			Title4
			CategoryC
			Title5
			グループ結果を絞る場合(Title、ViewCount)
			var articles = from a in _db.Article
			  group new ArticleView { Title = a.Title, ViewCount = a.ViewCount }
			  by a.Category;
			複数列でグループ化する場合(Title、ViewCount)
			var articles = from a in _db.Article
			  group a by new ArticleView { Title = a.Title, ViewCount = a.ViewCount };
			別クラス(Model/ArticleView.cs)
			public class ArticleView
			{
			  public string Title { get; set; }
			  public int ViewCount { get; set; }
			  public string Released { get; set; }
			}
		
			※View
			@model IEnumerable<IGrouping<string , ASPNET_MVC_DB.Models.Article>>
			<body>
			  //elmGrpにはカテゴリーのグループ結果が入る
			  @foreach (var elmGrp in Model)
			  {
			    @elmGrp.Key<br/>
			    //elmにはグループ内の要素が入る
			    foreach (var elm in elmGrp) {
			      @elm.Title<br/>
			    }
			    <br/>
			  }
			</body>
			⇒結果:
			CategoryA
			Title1
			Title2
			CategoryB
			Title3
			Title4
			CategoryC
			Title5
			グループ結果を絞る場合(Title、ViewCount)
			var articles = _db.Article
			  .GroupBy( a => a.Title , a => new ArticleView { Title = a.Title, ViewCount = a.ViewCount });
			複数列でグループ化する場合(Title、ViewCount)
			var articles = _db.Article
			  .GroupBy( a => new ArticleView { Title = a.Title, ViewCount = a.ViewCount });
			別クラス(Model/ArticleView.cs)
			public class ArticleView
			{
			  public string Title { get; set; }
			  public int ViewCount { get; set; }
			  public string Released { get; set; }
			}
		
  
  return View(articles);
}

