Created
December 3, 2015 11:24
-
-
Save itorian/19f4a871934b8f901ef4 to your computer and use it in GitHub Desktop.
EntityFramework vs LINQ vs ADO.NET - select query samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Entity Framework | |
public ActionResult GetBokaroStudentEF() | |
{ | |
var db = GetFromDbContext(); | |
var data = db.Students.Where(i => i.City == "Bokaro"); | |
return View(data); | |
} | |
// LINQ | |
public ActionResult GetBokaroStudentLINQ() | |
{ | |
var db = GetFromDbContext(); | |
var data = (from students in db.Students where students.City == "Bokaro" select students).ToList(); | |
return View(data); | |
} | |
// ADO.NET | |
public ActionResult GetBokaroStudentADONET() | |
{ | |
List<DataRow> data = null; | |
string srtQry = "SELECT * FROM TableName WHERE City = 'Bokaro'"; | |
string connString = "Database=yourDB;Server=yourServer;UID=user;PWD=password;"; | |
using (SqlConnection conn = new SqlConnection(connString)) | |
{ | |
using (SqlCommand objCommand = new SqlCommand(srtQry, conn)) | |
{ | |
objCommand.CommandType = CommandType.Text; | |
DataTable dt = new DataTable(); | |
SqlDataAdapter adp = new SqlDataAdapter(objCommand); | |
conn.Open(); | |
adp.Fill(dt); | |
if (dt != null) | |
{ | |
data = dt.AsEnumerable().ToList(); | |
} | |
} | |
} | |
return View(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment