Steps For Linq Setup in MVC When SQL Server Is Not Installed
Steps For Linq Setup in MVC When SQL Server Is Not Installed
1. Create Model classes and using code-first approach generate the database thru entity
framework
2. Create Homecontroller and access the Context class and write Linq queries
Steps
1. Add HomeController with Empty MVC template and change the name of the context class
as CustOrderContext
2.Add the models namespace
Eg: using MvcSingleTable.Models;
return View(q);
}
public ActionResult SimpleQry()
{
string names="";
var q = from s in db.Customers select
s;
foreach (var cust in q)
{
names = names + " " + cust.Name;
}
ViewBag.Names = names;
return View();
}
public ActionResult SimpleQryColumns()
{
string names = "";
var q = from s in db.Customers select
new { s.Name, s.City };
foreach (var cust in q)
{
names = names + " " + cust.Name + "
City:" + cust.City;
}
ViewBag.Names = names;
return View();
}
public ActionResult SimpleQryWhere()
{
string names = "";
var q = from s in db.Customers where
s.Name =="Kavitha" select s;
foreach (var cust in q)
{
names = names + " " + cust.Name;
}
ViewBag.Names = names;
return View();
}
public ActionResult ImmediateExecution()
{
ViewBag.Names = names;
return View();
}
<p> @ss</p>
---------------------------------------------part2
Add a class Product in Models folder
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
Add a ProductController
public ActionResult Index()
{
List<Product> list1 = new
List<Product>();
Product p1 = new Product();
p1.Name = "Biscut";
p1.Price = 200;
list1.Add(p1);
list1.Add(p2);
var data = from s in list1 select s;
//execute the query
foreach (var dd in data)
{
Response.Write(dd.Name);
Response.Write(dd.Price);
Response.Write("<BR/>");
}
ViewBag.pr = list1;
return View();
}
In the View
<p> @p.Name ,
@p.Price</p>
}