New to programming, I am tring to get the value of one cell from a table using c# in asp.net MVC; here what I am trying:
DALEntities db = new DALEntities();
var duration = db.MyTable.Where(c => c.Day == date && c.ID == id).Select(c => c.Duration);
Session["duration"] = duration;
to get the data to a view I use, in the controller:
var temp = Session[“duration”];
ViewBag.Duration = temp;
and in the view:
@ViewBag.Duration
I get
System.Data.Objects.ObjectQuery`1[System.String]
I would appreciate your suggestions.
You could call a
.First()at the end to eagerly execute the query and get the single result matching this criteria:Be careful though because if there are no records matching your
.Where()clause, the.First()extension method will throw an exception. In this case you might consider using the.FirstOrDefault()method.I also very strongly recommend you to learn basic LINQ before learning Entity Framework and mostly before learning ASP.NET MVC. Otherwise you will be struggling a lot with ASP.NET MVC. I also recommend to people learning separate frameworks in isolation without mixing them. Once you are familiar with them you could mix in one project.