DataTable products = myDataSet.Tables["products"]; DataTable classes = myDataSet.Tables["classes"];
内部连接
var result = from s in products // Inner Join表products和表classes,注意equals关键字 join c in classes on s.cid equals c.id select new { id = s.id, // 商品id name = s.name, // 商品名称 cname = c.name // 分类名 };
分组连接
var result = from s in products // 实际上就是内连接后按照cid进行分组 join c in classes on s.cid equals c.id into G select new { id = G.Key, // 分组的cid name = c.name, // 分类名 list = G // 组成员,组成员为连接后的记录,字段要比之前多 };
左外连接
var result = from c in classes join s in products on c.id = s.cid into G from item in G.DefaultIfEmpty() // 当右侧products表无法匹配左表classes时,赋null select new { cname = c.name, list = item; // item可能为null };
为null的item赋默认值
var result = from c in classes join s in products on c.id = s.cid into G from item in G.DefaultIfEmpty(new product{id=0, name="", cid=0}) // 当右侧products表无法匹配左表classes时,赋新建的product对象 select new { cname = c.name, list = item; };
声明:如需转载,请注明来源于www.webym.net并保留原文链接:http://www.webym.net/jiaocheng/767.html