Sunday, June 2, 2019

Linq Delete with EntityState

1.
public ActionResult Delete(int id)
{
    using (var cont = new db_context())
    {
        var emp = new Employee()
        {
            emp_id = id
        };
        cont.Entry(emp).State = System.Data.Entity.EntityState.Deleted;
        cont.SaveChanges();
    }
    return RedirectToAction("index");
}

LINQ Status with Any and condition in firstOrDefault

1. Status by Any
     bool status = db.Employee.Any(x => x.emp_id == 1);

2. Condtion in FirstOrDefault
     Employee employee = db.Employee.FirstOrDefault(x => x.emp_id == 1);

Linq Fill Directecly model in linq query


With Where condition

Top 100 in Linq

Linq Expression syntax for where condtion in linq

(Expression<Func<T, bool>> filter)