Click here to Skip to main content
Click here to Skip to main content

Joining Table and Display Data using WCF Service in MVC

, 21 Dec 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to join table and display data using WCF Service in MVC
  1. Create a new Webapplication in MVC.

  2. Create a new database and add two tables which are department_table and employee.

    In Employee table, Did field is foreign key of Department_table.

  3. Now add entity model in website.

  4. In which choose employee and department_table.
  5. Add new Class in which write this field that you want to display in website.

  6. Add WCF Service to website and in Iservice.cs file, write this code:
        List<Class1> getdata();
  7. 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();
  8. Now Run Service. Then add other MVC website in which also add class which is previously added in website.
  9. 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,
              return View(data);
  10. Output of this code is as follows:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shah Ankita
Software Developer
India India
No Biography provided
Follow on   LinkedIn

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 21 Dec 2014
Article Copyright 2014 by Shah Ankita
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid