Display value in Database DataContext

158 views Asked by At

This is my Query =>

var department = dataContext.Query<Department>("SELECT Id FROM tblDepartment");

I want to display the values in variable Department in ViewBag or Anywhere.How can I do that?

2

There are 2 answers

0
teo van kot On BEST ANSWER
var department = dataContext.Query<Department>("SELECT Id FROM tblDepartment").ToList() 

and then work with list

2
Saket Kumar On

You can use linq to Entities without executing raw queries as:

var department = dataContext.Department.Select(x=>x.Id).ToList();

If you are stubborn to use raw query, you can use as teo van kot has suggested above:

var department = dataContext.Query<Department>("SELECT Id FROM tblDepartment").ToList();

You can assign the list to ViewBag in controller as:

ViewBag.DepartmentIDs = department;

You can display the values in View as:

@{

foreach(var item in ViewBag.DepartmentIDs)
{
<span>@item <br/></span>
}

}