关于LINQ问题!!!!

于娜 2010-09-13 11:05:42
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的信息呢?


...全文
114 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cp58284966 2010-09-13
  • 打赏
  • 举报
回复
select * 打错了
不懂装懂 2010-09-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]
var gl = from p in PersonList join car in CarList on p.cid equals car.cid select p;
===
var gl = from p in PersonList join car in CarList on p.cid equals car.cid
select new{p.age,p.Name,car.id,ca……
[/Quote]
+1
http://msdn.microsoft.com/zh-cn/library/bb399391.aspx
cp58284966 2010-09-13
  • 打赏
  • 举报
回复
form *
q107770540 2010-09-13
  • 打赏
  • 举报
回复
var gl = from p in PersonList join car in CarList on p.cid equals car.cid select p;
===
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}
不懂装懂 2010-09-13
  • 打赏
  • 举报
回复
传说中的娜姐也开始做技术了?

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧