linq关联查询

#region 定义类型 /// <summary> /// 图书分类信息 /// </summary> public class Category { /// <summary> /// 分别ID /// </summary> public int catID { get; set; } /// <summary> /// 分类名 /// </summary> public string catName { get; set; } } /// <summary> /// 图书信息 /// </summary> public class BookInfo { /// <summary> /// 图书ID /// </summary> public int BookID { get; set; } /// <summary> /// 书名 /// </summary> public string BookName { get; set; } /// <summary> /// 图书所属分类的ID /// </summary> public int CateID { get; set; } } #endregion class Program { static void Main(string[] args) { // 图书分类示例数据 List<Category> bookCategs = new List<Category> { new Category { catID = 201, catName = "文学类" }, new Category { catID = 202, catName = "经济管理类" }, new Category { catID = 203, catName = "机械工程类" }, new Category { catID = 204, catName = "法律基础类" } }; // 图书信息示例数据 List<BookInfo> books = new List<BookInfo> { new BookInfo { BookID = 1, BookName = "图书01", CateID = 202 }, new BookInfo { BookID = 2, BookName = "图书02",CateID = 204 }, new BookInfo { BookID = 3, BookName = "图书03", CateID = 201 }, new BookInfo { BookID = 4, BookName = "图书04", CateID = 202 }, new BookInfo { BookID = 5, BookName = "图书05",CateID = 204 }, new BookInfo { BookID = 6, BookName = "图书06", CateID = 204 }, new BookInfo { BookID = 7, BookName = "图书07", CateID = 203 }, new BookInfo { BookID = 8, BookName = "图书08",CateID = 202 }, new BookInfo { BookID = 9, BookName = "图书09", CateID = 203 }, new BookInfo { BookID = 10, BookName = "图书10", CateID = 202 }, new BookInfo { BookID = 11, BookName = "图书11", CateID = 201 }, new BookInfo { BookID = 12, BookName = "图书12", CateID = 203 }, new BookInfo { BookID = 13, BookName = "图书13", CateID = 201 }, new BookInfo { BookID = 14, BookName = "图书14", CateID = 204 }, new BookInfo { BookID = 15, BookName = "图书15", CateID = 203 }, new BookInfo { BookID = 16, BookName = "图书16", CateID = 202 }, }; // 联合查询,并产生新的类型 var qryres = from b in books join c in bookCategs on b.CateID equals c.catID select new { b.BookName, c.catName }; // 输出结果 foreach (var bitem in qryres) { Console.WriteLine("图书名:{0},所属分类:{1}", bitem.BookName, bitem.catName); } Console.WriteLine(); // 第二个联合查询 var qryres2 = from bk in books join bc in bookCategs on bk.CateID equals bc.catID select new { Book_ID = bk.BookID, Book_Name = bk.BookName, Book_Cate = bc.catName }; // 输出结果 foreach (var bk in qryres2) { Console.WriteLine("图书ID:{0},图书名:{1},所属分类:{2}", bk.Book_ID, bk.Book_Name, bk.Book_Cate); } Console.Read(); } }

转载于:https://www.cnblogs.com/xuyuchen/p/8282977.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值