#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