Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save itorian/19f4a871934b8f901ef4 to your computer and use it in GitHub Desktop.
Save itorian/19f4a871934b8f901ef4 to your computer and use it in GitHub Desktop.
EntityFramework vs LINQ vs ADO.NET - select query samples
// 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