ASP. NET MVC traversal LINQ to SQL multi-table joint search

Source: Internet
Author: User
Tags actionlink

Two tables: class table and student table: finally want to obtain the student's name, password, gender, age, address, hobby, class name, the direction of the class

var temp=from a in _db.studentjoin B in _db.classesOn a.c_id equals b.IDSelectNew                           {Id=a.id,Name = A.name,Pwd = a.pwd,Sex = A.sex,Age = A.age,Address = a.address,Hobby = A.hobby,stuname = b.name,Direction = b.direction

}; using LINQ to SQL for multi-table linking, the problem is, here is the type of push, how the foreground binding is a problem then can you create a class that contains these fields?

Public class Stu    {public int Id {get; set;}Public string Name {get; set;}Public string Pwd {get; set;}Public bool? Sex {get; set;}public int? Age {get; set;}Public string Address {get; set;}Public string Hobby {get; set;}Public string Stuname {get; set;}Public string Direction {get; set;}

then select New Stu () {} specific code for multi-table search:

var temp=from a in _db.studentjoin B in _db.classesOn a.c_id equals b.IDSelectnew Stu ()                           {Id=a.id,Name = A.name,Pwd = a.pwd,Sex = A.sex,Age = A.age,Address = a.address,Hobby = A.hobby,stuname = b.name,Direction = b.direction

                           };   so you can return a iqueryable.elementtype{name= "Stu" , fullname= "project name. models.stu"} viewdata.model = temp. ToList (); This translates to Ienumerable<cool. models.stu> @model IEnumerable <cool. models.stu>   @foreach (var item in Model) for the effect: span>

Public class Homecontroller:controller    {readonly studentdatacontext _db=new studentdatacontext ();//GET:/home/Public ActionResult Index ()        {var temp=from a in _db.studentjoin B in _db.classesOn a.c_id equals b.IDSelectnew Stu ()                           {Id=a.id,Name = A.name,Pwd = a.pwd,Sex = A.sex,Age = A.age,Address = a.address,Hobby = A.hobby,stuname = b.name,Direction = b.direction                           };Viewdata.model = temp. ToList ();            return View ();        }     }full code for view:@model Ienumerable<cool. Models.stu>@using System.Collections@{Layout = null;}<! DOCTYPE html><title>Index</title><body><p>@Html. ActionLink ("Create New", "create")</p><table border= "1" cellpadding= "0" cellspacing= "0" ><tr><th></th><th>name</th><th>Password</th><th>Sex</th><th>Age</th><th>Address</th><th>Hobbies</th><th>class name</th><th>Professional Direction</th></tr>    @foreach (var item in Model) {<tr><td>@* @Html. ActionLink ("edit", "edit", new {Id=item). Id}) |@Html. ActionLink ("Details", "details", new {Id=item. Id}) |@Html. ActionLink ("delete", "delete", new {Id=item. Id}) *@</td><td>@item. Name</td><td>@item. PWD</td><td>@ (item. Sex==true? " Male ":" female ")</td><td>@item. Age</td><td>@item. Address</td><td>@item. Hobby</td><td>@item. Stuname</td><td>@item. Direction</td></tr>    }    </table></body>

ASP. NET MVC traversal LINQ to SQL multi-table joint search

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.