1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class Product { public string Name { get; set; } public int CategoryID { get; set; } }
class Category { public string Name { get; set; } public int ID { get; set; } }
class ProductWithCategoryName { public string ProductName { get; set; } public string CategoryName { get; set; } } static void Main(string[] args) { List<Category> categories = new List<Category>() { new Category {Name="Beverages", ID=001}, new Category {Name="Condiments", ID=002}, new Category {Name="Vegetables", ID=003}, new Category {Name="Grains", ID=004}, new Category {Name="Fruit", ID=005} };
List<Product> products = new List<Product>() { new Product {Name="Cola", CategoryID=001}, new Product {Name="Tea", CategoryID=001}, new Product {Name="Mustard", CategoryID=002}, new Product {Name="Pickles", CategoryID=002}, new Product {Name="Carrots", CategoryID=003}, new Product {Name="Bok Choy", CategoryID=003}, new Product {Name="Peaches", CategoryID=005}, new Product {Name="Melons", CategoryID=005}, }; var productWithCategoryNameList = products.Join(categories, p => p.CategoryID, c => c.ID, (p, c) => new ProductWithCategoryName { ProductName = p.Name, CategoryName = c.Name, }.ToList());
}
|