using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ List<Person> PersonList = new List<Person>(); //创建List类
PersonList.Add(new Person(21, "公务员", 1)); //购买车ID为1的人
PersonList.Add(new Person(22, "城管", 2)); //购买车ID为2的人
PersonList.Add(new Person(23, "贪官", 2)); //购买车ID为3的人 List<CarInformaion> CarList = new List<CarInformaion>(); //创建List类
CarList.Add(new CarInformaion(1, "宝马"));
CarList.Add(new CarInformaion(2, "奔驰"));
var gl = from p in PersonList join car in CarList on p.cid equals car.cid select p;//使用join子句
foreach (var element in gl) //遍历集合
{
Response.Write(element.name.ToString()+"<br/>"); //输出对象
}
}
} public class Person //描述“人”对象
{
public int age; //描述“年龄”字段
public string name; //描述“姓名”字段
public int cid; //描述“车ID”字段
public Person(int age, string name, int cid)
{
this.age = age;
this.name = name;
this.cid = cid;
}
} public class CarInformaion //描述“车”对象
{
public int cid; //描述“车ID”字段
public string type; //描述“车类型”字段
public CarInformaion(int cid, string type)
{
this.cid = cid;
this.type = type;
}
}
}上面的代码,大家一看就知道啥意思,var gl = from p in PersonList join car in CarList on p.cid equals car.cid select p;//使用join子句
我有个问题,最后的 select p 只能获取Person的信息,如何返回关联表的Car的信息呢?
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ List<Person> PersonList = new List<Person>(); //创建List类
PersonList.Add(new Person(21, "公务员", 1)); //购买车ID为1的人
PersonList.Add(new Person(22, "城管", 2)); //购买车ID为2的人
PersonList.Add(new Person(23, "贪官", 2)); //购买车ID为3的人 List<CarInformaion> CarList = new List<CarInformaion>(); //创建List类
CarList.Add(new CarInformaion(1, "宝马"));
CarList.Add(new CarInformaion(2, "奔驰"));
var gl = from p in PersonList join car in CarList on p.cid equals car.cid select p;//使用join子句
foreach (var element in gl) //遍历集合
{
Response.Write(element.name.ToString()+"<br/>"); //输出对象
}
}
} public class Person //描述“人”对象
{
public int age; //描述“年龄”字段
public string name; //描述“姓名”字段
public int cid; //描述“车ID”字段
public Person(int age, string name, int cid)
{
this.age = age;
this.name = name;
this.cid = cid;
}
} public class CarInformaion //描述“车”对象
{
public int cid; //描述“车ID”字段
public string type; //描述“车类型”字段
public CarInformaion(int cid, string type)
{
this.cid = cid;
this.type = type;
}
}
}上面的代码,大家一看就知道啥意思,var gl = from p in PersonList join car in CarList on p.cid equals car.cid select p;//使用join子句
我有个问题,最后的 select p 只能获取Person的信息,如何返回关联表的Car的信息呢?
===
var gl = from p in PersonList join car in CarList on p.cid equals car.cid
select new{p.age,p.Name,car.id,car.type}
http://msdn.microsoft.com/zh-cn/library/bb399391.aspx