linq 关联查询

  

   可得会在以后的实体类中能用到
   
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace test2
{
    
class Student
    
{
        
public string First getset; }
        
public string Last getset; }
        
public int ID getset; }
        
public string Street getset; }
        
public string City getset; }
        
public List<int> Scores;  

    }


    
class Teacher
    
{
        
public string First getset; }
        
public string Last getset; }
        
public int ID getset; }
        
public string City getset; }
      
    }


    
public partial class _Default : System.Web.UI.Page
    
{




        
protected void Page_Load(object sender, EventArgs e)
        
{
            List
<Student> students = new List<Student>()
        
{
            
new Student {First="",
                Last
=""
                ID
=111
                Street
="123 Main Street",
                City
="太原",
                Scores
= new List<int> {97928160}}
,
            
new Student {First="",
                Last
=""
                ID
=112,
                Street
="124 Main Street",
                City
="太原",
                Scores
= new List<int> {75849139}}
,
            
new Student {First="",
                Last
="",
                ID
=113,
                Street
="125 Main Street",
                City
="上海",
                Scores
= new List<int> {88946591}}
,
        }
;

   List
<Teacher> teachers = new List<Teacher>()
        
{                
            
new Teacher {First="", Last="老师", ID=945, City = "太原"},
            
new Teacher {First="", Last="老师", ID=956, City = "郑州"},
            
new Teacher {First="", Last="老师", ID=972, City = "北京"}
        }
;

            
// 相当于sql union 关联
            var peopleInSeattle = (from student in students
                                   where student.City 
== "太原"
                                   select student.First
+student.Last)
                                 
                           .Concat(from teacher 
in teachers
                                where teacher.City 
== "太原"
                                select teacher.First
+teacher.Last);



            
this.Response.Write("老师和学生都生活在太原的是:<br />");
            
// Execute the query.
            foreach (var person in peopleInSeattle)
            
{
                
this.Response.Write(person + "<br />");
              
            }
              //相当于sql inner
            var query 
= students.AsQueryable().Join(teachers,
                        student 
=> student.City,
                        teacher 
=>teacher.City,
          (student, teacher) 
=>new { 老师姓名 = teacher.First+teacher.Last, 城市 = student.City,学生姓名=student.First+student.Last });
            
            
this.Response.Write("老师和学生都生活在同一城市的是:<br />");
            
foreach (var obj in query)
            
{
                
this.Response.Write(obj.老师姓名  + ","+ obj.城市+","+obj.学生姓名+"<br/>");
            }

       
        }

    }

posted on 2007-12-14 09:08  gwazy  阅读(3179)  评论(1编辑  收藏  举报

导航