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