Linq to entityノート
3312 ワード
1.指定したフィールドに戻ってエンティティを生成
2.挿入:効率が悪い方法は開拓しなければならない、向上する
List<Product> q = db.Product.Where(a => a.ProductName == "a ")
.Select(a => new { ProductName = a.ProductName, AddTime = a.AddTime }).ToList() // ToList() SQL
.Select(a => new Product() { ProductName = a.ProductName, AddTime = a.AddTime }).ToList();
//
/* SELECT
1 AS [C1],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[AddTime] AS [AddTime]
FROM [dbo].[Product] AS [Extent1]
WHERE N'a' = [Extent1].[ProductName] */
foreach (Product p in q)
{
Console.WriteLine(p.ProductName);
}
2.挿入:効率が悪い方法は開拓しなければならない、向上する
Product pro = new Product();
pro.ProductName = "I lOVER YOU";
pro.ProductPrice = 12m;
pro.AddTime = DateTime.Now;
db.AddToProduct(pro);
pro = new Product();
pro.ProductName = "I lOVER YOU";
pro.ProductPrice = 12m;
pro.AddTime = DateTime.Now;
db.AddToProduct(pro);
db.SaveChanges();
/*
exec sp_executesql N'insert [dbo].[Product]([ProductName], [ProductPrice], [ProductDisc], [AddTime])
values (@0, @1, null, @2)
select [ProductID]
from [dbo].[Product]
where @@ROWCOUNT > 0 and [ProductID] = scope_identity()',N'@0 nvarchar(12),@1 decimal(18,0),@2 datetime',@0=N'I lOVER YOU',@1=12,@2='2012-03-15 23:05:21.860'
go
exec sp_executesql N'insert [dbo].[Product]([ProductName], [ProductPrice], [ProductDisc], [AddTime])
values (@0, @1, null, @2)
select [ProductID]
from [dbo].[Product]
where @@ROWCOUNT > 0 and [ProductID] = scope_identity()',N'@0 nvarchar(12),@1 decimal(18,0),@2 datetime',@0=N'I lOVER YOU',@1=12,@2='2012-03-15 23:05:21.860'
go
*/