Click Here For MCQ

Monday, June 22, 2020

linq formula in c#

public ActionResult Details(string requestid)
    {
        var entities = new EmployDBEntities1();
       var detailsModel = entities.Details.Single(e => e.Id == requestid);
       return View(detailsModel);
        //return View(entities.Details.ToList());
    }
public ActionResult Details(int countryid)
    {
list<states> statelist=db.tbl_student.where(x => x.countryid==countryid).tolist();
}
 if (search != null)
            {   
                var  model = employees.Where(x => x.Name.Contains(search) || x.Email.Contains(search)).ToList();
                return View(model);
            }

employees = employees.OrderBy(x => x.Name).ToList();


 employees = employees.OrderByDescending(x => x.Name).ToList();

Easiest way to check record is exist or not in Linq to Entity Query


 bool exist=db.UserDetails.Any(x=>x.id==1);






if you using first and first or default ......search result is same ......

but if you dont know record is present or not you must use firstordefault it will give null if record is not present ......but in first() is use ,then record not found then it will give exception in program ......so if you record is definately present so you can use first() .
UserDetail _U=db.UserDetails.where(x=>x.id==1).FirstOrDefault();
UserDetail _U=db.UserDetails.where(x=>x.id==1).First();



No comments:

Post a Comment