Nasıl yapılır: Join Tümcesinin Sonuçlarını Sıralama (C# Programlama Kılavuzu)
Bu örnek, birleştirme işleminin sonuçları gösterilmiştir.Not sıralama birleştirme sonrasında gerçekleştirilir.Siz kullanabilirsiniz, ancak bir orderby yan tümcesi ile bir veya daha fazla kaynak sequences birleştirme öncesinde, genellikle bunu önermiyoruz.Bazı LINQ sağlayıcıları değil korumak sonra birleştirme sıralaması.
Örnek
Bu sorgu bir grup birleşim oluşturur ve hala kapsamında Kategori öğesinin temel grupları sıralar.Anonim tür başlatıcısı içinde bir sorgu ürün serisinden eşleşen tüm öğeleri sıralar.
class HowToOrderJoins
{
#region Data
class Product
{
public string Name { get; set; }
public int CategoryID { get; set; }
}
class Category
{
public string Name { get; set; }
public int ID { get; set; }
}
// Specify the first data source.
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}
};
// Specify the second data source.
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},
};
#endregion
static void Main()
{
HowToOrderJoins app = new HowToOrderJoins();
app.OrderJoin1();
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
void OrderJoin1()
{
var groupJoinQuery2 =
from category in categories
join prod in products on category.ID equals prod.CategoryID into prodGroup
orderby category.Name
select new
{
Category = category.Name,
Products = from prod2 in prodGroup
orderby prod2.Name
select prod2
};
foreach (var productGroup in groupJoinQuery2)
{
Console.WriteLine(productGroup.Category);
foreach (var prodItem in productGroup.Products)
{
Console.WriteLine(" {0,-10} {1}", prodItem.Name, prodItem.CategoryID);
}
}
}
/* Output:
Beverages
Cola 1
Tea 1
Condiments
Mustard 2
Pickles 2
Fruit
Melons 5
Peaches 5
Grains
Vegetables
Bok Choy 3
Carrots 3
*/
}
Kod Derleniyor
Oluşturma bir Visual Studio hedefleyen bir proje.net Framework sürüm 3.5.Varsayılan olarak, proje başvuru System.Core.dll sahiptir ve bir using System.Linq ad alanı için yönerge.
Projenize kodu kopyalayın.
Derlemek ve program çalıştırmak için F5 tuşuna basın.
Konsol penceresine çıkmak için herhangi bir tuşa basın.
Ayrıca bkz.
Başvuru
orderby tümcesi (C# Başvurusu)