Free Web Development Online Tutorials, Learn to Code
Learn LINQ with C#, Language Integrated query with different Data Source

LINQ with Entityframework

Learn to use LINQ with Entityframework

In this article we learn how to write LINQ query with Entity Framework, so it will be easy to understand if you are already familiar with Entity Framework.

Entity framework supports three types of queries:

  1. LINQ to Entities
  2. Entity SQL
  3. Native SQL

LINQ to Entity is very popular for rapid application development, especially with Database First approach, LINQ to Entities does not have any limitation for data source like LINQ to SQL, in Entity Framework we can work with any data source like Oracle, My SQL etc.

Here you learn how to perform CRUD Operation (Create, Read, Update, Delete) using LINQ to Entities, as we have already published exclusive tutorials on Entity Framework

Create or Add data example

public tbStudent AddStudent(tbStudent student)            
{
    tbStudent _student = null;
    using (Entities context = new Entities())
    {
        _student = context.tbStudents.Add(student);
        context.SaveChanges();
    }
return _student;
}

Read or Select data example

public List<tbStudent> getAllStudents()
{
    List<tbStudent> _students = null;

    using (Entities context = new Entities())
    {
        _students = context.tbStudents
        .ToList<tbStudent>();
    }
    return _students;
}

Querying with LINQ to Entities, Read or Select data with Where clause example

public tbStudent getStudent(int id)
{
tbStudent _student = null;
using (Entities context = new Entities())
{
_student = context.tbStudents
.Where(s => s.studentId == id)
        .FirstOrDefault<tbStudent>();
    }
return _student;
}

Another way to write

using (Entities context = new Entities())
{
    var student = (from s in context.tbStudents
                where s.FullName == "Anu"
                select s).FirstOrDefault<tbStudent>();
}

Update or Edit data in Entity Framework, notice State = EntityState.Modified;

public bool updateStudent(tbStudent student)
{
tbStudent _student = null;
bool _isDone = false;
using (Entities context = new Entities())
{
_student = context.tbStudents
.Where(s => s.studentId == student.studentId)
        .FirstOrDefault<tbStudent>();

            if (_student != null)
            {
            // updating new values
            _student.FullName = student.FullName;
            _student.email = student.email;
            context.Entry<tbStudent>(_student).State = EntityState.Modified;
            context.SaveChanges();
            _isDone = true;
            }
    }
return _isDone;
}

Delete Data with Where clause using Entity Framework Example, notice State = EntityState.Deleted;

public bool deleteStudent(int id) {
tbStudent _student = null;
bool _isDeleted = false;
using (Entities context = new Entities())
{
_student = context.tbStudents
.Where(s => s.studentId == id)
    .FirstOrDefault<tbStudent>();
            
        if (_student != null)
        {
        context.Entry<tbStudent>(_student).State = EntityState.Deleted;
        context.SaveChanges();
        _isDeleted = true;
        }
    }
return _isDeleted;
}
Comment
Name
Email
Website
Subscribe
 
Learn LINQ .Net

LINQ to Entity framework Example in C#

linq Interview Questions Answers

LINQ C# examples | Join Asp.net MVC Course