0% found this document useful (0 votes)
2 views5 pages

Steps For Linq Setup in MVC When SQL Server Is Not Installed

The document outlines the steps for implementing LINQ in an MVC application without SQL Server, starting with creating model classes using a code-first approach and generating a database through Entity Framework. It includes detailed instructions for creating controllers, executing LINQ queries, and displaying results in views. Additionally, it describes adding a Product model and controller to manage product data within the application.

Uploaded by

vijaya2511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Steps For Linq Setup in MVC When SQL Server Is Not Installed

The document outlines the steps for implementing LINQ in an MVC application without SQL Server, starting with creating model classes using a code-first approach and generating a database through Entity Framework. It includes detailed instructions for creating controllers, executing LINQ queries, and displaying results in views. Additionally, it describes adding a Product model and controller to manage product data within the application.

Uploaded by

vijaya2511
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Steps for Linq

Procedure if no sql server is 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. Right click Models folder from a Basic MVC application


2. Add Class –Customer and add the below code

public class Customer


{
public int CustomerID { get; set; }
public string Name { get; set; }
public string City { get; set; }

public virtual ICollection<Order> Orders { get; set; }


}

3. Add another class Order

public class Order


{
public int OrderID { get; set; }
public string ProductName { get; set; }
public int UnitPrice { get; set; }
public int Qty { get; set; }
public int CustomerID { get; set; }

public virtual Customer Customer { get; set; }


}

4. Add another class

public class CustOrderContext:DbContext


{
public CustOrderContext() : base("DefaultConnection") { }
public DbSet<Customer> Customers { get; set; }
public DbSet<Order> Orders {get;set;}
}
5. Save and Build the project
6. Right click Controller , add CustomerController with Model class Controller and context as
CustOrderContext
7. Right click Controller , add OrderController with Model class Controller and context as
CustOrderContext
8. Save ,Build and Run the project
9. Add values in customer and orders.
Part2

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;

CustOrderContext db = new CustOrderContext();

public ActionResult Index()


{
var q = from s in db.Customers select s;

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()
{

//List<string> names = ( from s in


db.Customers select s.City).ToList();
string[] names = (from s in db.Customers
select s.Name).ToArray();

ViewBag.Names = names;
return View();
}

Index Action method is a strongly typed view

All the other view type the following code


<p>@ViewBag.Names</p>

For ImmdeiateExecution view


<h2>ImmediateExecution</h2>
@foreach (var ss in @ViewBag.Names)
{

<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;

Product p2 = new Product();


p2.Name = "Biscut";
p2.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

@foreach (var p in ViewBag.pr)


{

<p> @p.Name ,
@p.Price</p>
}

You might also like