Joining Table and Display Data using WCF Service in MVC






4.71/5 (3 votes)
How to join table and display data using WCF Service in MVC
- Create a new
Webapplication
in MVC. - Create a new database and add two tables which are
department_table
andemployee
.In
Employee
table,Did
field is foreign key ofDepartment_table
. - Now add entity model in website.
- In which choose
employee
anddepartment_table
. - Add new Class in which write this field that you want to display in website.
- Add WCF Service to website and in Iservice.cs file, write this code:
[OperationContract] List<Class1> getdata();
- In Service.cs file, write this code:
DatabaseEntities obj = new DatabaseEntities(); public List<Class1> getdata() { var x = from b in obj.Employees join a in obj.Department_table on b.Did equals a.Did select new Class1 { Name = b.Name, Address = b.Address, Department = a.Department }; return x.ToList(); }
- Now Run Service. Then add other MVC website in which also add class which is previously added in website.
- Add
ServiceReference
and in home controller, add this code:ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient(); public ActionResult Index() { var x=obj.getdata(); var y = x.ToList(); var data = new List<Class1>(); foreach(var item in y) { data.Add(new Class1 { Name = item.Name, Address=item.Address, Department=item.Department }); } return View(data); }
- Output of this code is as follows: