码迷,mamicode.com
首页 > Web开发 > 详细

MVC+Linq+EF

时间:2015-06-01 12:52:40      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

using (DataContext ctx = new DataContext())
{
/*foreach (var item in ctx.employees)
{
ViewData["Name"] = item.Name;
}
*/
//------------------------------------------
/*var employees = from emp in ctx.employees
where emp => emp.Age == 22 && emp.ID == "123456781"
select emp;
//上边转化
var employees = ctx.employees.Where(emp => emp.Age == 22 && emp.ID == "123456781");
foreach (var item in employees)
{
ViewData["Name"] = item.Name;
}--------------------------------------------*/
/*---------------------------------------
* var employees = from emp in ctx.employees
where emp.Age == 22 && emp.ID == "123456781"
select emp.Name;//查询单个字段
*/
var employees = ctx.employees.Where(emp => emp.Age == 22 && emp.ID == "123456781")
.OrderBy(emp => emp.ID)
.Select(emp => emp.Name);//查询单个字段
//---------------------------------------------------
/*
var employees2 = from emp in ctx.employees
where emp.Age == 21
select new { emp.Age, emp.Name };//查询多个字段*/
var employees2 = ctx.employees.Where(emp => emp.Age == 21)
.OrderBy(emp => emp.Age)
.Select(emp => new { emp.Name, emp.Age });//查询多个字段*/
//----------------------------------------------
//不管怎样都要用foreach、
foreach (var item in employees)
{
ViewData["single"] = item;//因为它查出来是单个字段,所以直接输出,不用点了
}
foreach (var item in employees2)
{
ViewData["doubleAge"] = item.Age;
ViewData["doubleName"] = item.Name;//他很智能的。
}
return View();
}

MVC+Linq+EF

标签:

原文地址:http://www.cnblogs.com/jinhaoObject/p/4543524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!