2016-03-25 20 views
-2

aşağıdaki şekilde C# bir listesi var:Çoklu ve nullable sütunlarla gruplandır, C# Linq?

Ben linq ile aşağıdaki sonucu elde edebilirsiniz nasıl
COL1 COL2 COL3 COL4 COL5 COL6 COL7 
---- ---- ---- ---- ---- ---- ---- 
1  8635 16  NULL Design 64  Device type 
1  8635 16  NULL Design 65  OS 
1  8635 16  NULL Design 66  Form factor 
1  8635 16  NULL Design 67  Dimensions 
---- ---- ---- ---- ---- ---- ----   
1  8635 17  NULL Design1 64  Device type 
1  8635 17  NULL Design1 65  OS 
1  8635 17  NULL Design1 66  Form factor 
1  8635 17  NULL Design1 67  Dimensions 

?

Group1: 
Keys: 
    1  8635 16  NULL Design 
Items: 
    64  Device type 
    65  OS 
    66  Form factor 
    67  Dimensions 

Group2: 
Keys: 
    1  8635 17  NULL Design1 
Items: 
    64  Device type 
    65  OS 
    66  Form factor 
    67  Dimensions 

ben aşağıdaki gibi yaptım ama 8 öğelerle sadece bir grup döndürür: group by kullanmak

var groupedItems = myDataList 
       .GroupBy(q => 
        new 
        { 
         q.Col1, 
         q.Col2, 
         q.Col3, 
         q.Col4, 
         q.Col5 
        }).ToList(); 

Gerçek sınıf Entity Framework bir Görünüm, ben 7 sütun yazdı onun.
Ben 7 ilk sütunlara göre gruplandırmak istiyorum: ObjectId, DeviceId, DeviceSpecificationCategoryId, DeviceSpecificationCategoryIsHidden, DeviceSpecificationCategoryName, DeviceSpecificationCategoryPersianName, DeviceSpecificationCategoryOrderNumberInDevicePage

[EntityFlag] 
public partial class DevicePresentationView : BaseEntity 
{ 
    [PrimaryKey] 
    public int ObjectId { get; set; } 
    [PrimaryKey] 
    public int DeviceId { get; set; } 
    public Nullable<int> DeviceSpecificationCategoryId { get; set; } 
    public Nullable<bool> DeviceSpecificationCategoryIsHidden { get; set; } 
    public string DeviceSpecificationCategoryName { get; set; } 
    public string DeviceSpecificationCategoryPersianName { get; set; } 
    public Nullable<int> DeviceSpecificationCategoryOrderNumberInDevicePage { get; set; } 
    public Nullable<int> DeviceSpecificationItemId { get; set; } 
    public string DeviceSpecificationItemName { get; set; } 
    public string DeviceSpecificationItemPersianName { get; set; } 
    public Nullable<bool> DeviceSpecificationItemIsHidden { get; set; } 
    public Nullable<int> DeviceSpecificationItemOrderNumberInDevicePage { get; set; } 
    public string DeviceSpecificationItemDescription { get; set; } 
    public Nullable<bool> DeviceSpecificationItemIsPrimary { get; set; } 
    public Nullable<bool> DeviceSpecificationItemIsEssential { get; set; } 
    public string DeviceSpecificationItemUnitName { get; set; } 
    public string DeviceSpecificationItemUnitPersianName { get; set; } 
    public Nullable<int> DeviceSpecificationItemValueTypeId { get; set; } 
    public Nullable<long> DeviceSpecificationValueId { get; set; } 
    public string DeviceSpecificationValue { get; set; } 
    public Nullable<double> DeviceSpecificationNumericValue { get; set; } 
    public Nullable<int> DeviceBenchmarkCategoryId { get; set; } 
    public Nullable<bool> DeviceBenchmarkCategoryIsHidden { get; set; } 
    public string DeviceBenchmarkCategoryName { get; set; } 
    public string DeviceBenchmarkCategoryPersianName { get; set; } 
    public Nullable<int> DeviceBenchmarkCategoryOrderNumberInDevicePage { get; set; } 
    public Nullable<int> DeviceBenchmarkCategoryParentId { get; set; } 
    public string DeviceBenchmarkCategoryDescription { get; set; } 
    public Nullable<int> DeviceBenchmarkItemId { get; set; } 
    public string DeviceBenchmarkItemName { get; set; } 
    public Nullable<bool> DeviceBenchmarkItemIsHidden { get; set; } 
    public string DeviceBenchmarkItemPersianName { get; set; } 
    public Nullable<int> DeviceBenchmarkItemOrderNumberInDevicePage { get; set; } 
    public string DeviceBenchmarkItemDescription { get; set; } 
    public Nullable<bool> DeviceBenchmarkItemIsPrimary { get; set; } 
    public string DeviceBenchmarkItemUnitName { get; set; } 
    public string DeviceBenchmarkItemUnitPersianName { get; set; } 
    public Nullable<int> DeviceBenchmarkItemValueTypeId { get; set; } 
    public Nullable<long> DeviceBenchmarkValueId { get; set; } 
    public string DeviceBenchmarkValue { get; set; } 
    public Nullable<double> DeviceBenchmarkNumericValue { get; set; } 
    public Nullable<long> DeviceBenchmarkAttachmentId { get; set; } 
} 
+0

Sorgunuzda ok ve 2 grupları dönmelidir. Sınıfınızı ve 'myDataList’in başlatılmasını gönderebilir misiniz? –

+0

Tamam, Düzenlenmiş gönderiyi –

+0

görebilirsiniz. Ancak, birincil anahtar olan 'ObjectId 've' DeviceId' bileşenlerini eklerseniz, o zaman benzersiz bir şekilde, o gruptaki satır sayısıyla tam olarak aynı sayıda grup elde edersiniz. tarafından. –

cevap

1

Sen bunun için de, muhtemelen cevapsız Col5 üzerine gruba sahip oldukları:

var groupedItems = myDataList 
       .GroupBy(q => 
        new 
        { 
         q.Col1, 
         q.Col2, 
         q.Col3, 
         q.Col4, 
         q.Col5 // notice this 
        }).ToList(); 

Artık Anahtarlar ve Grupla Grupla yazdırmak veya değiştirmek için sonuç üzerinde yineleyebilirsiniz.

ve istenilen gibi sonuç döndürür:

enter image description here

+0

Hızlı yanıtladığınız için teşekkürler, ancak yazım hatamdı, lütfen düzenlenmiş gönderiyi inceleyin –

+0

bunları nasıl görüntülüyorsunuz? –

+0

aşağıdaki olarak gösterilecek: 'foreach (groupedItems olarak var grubu) { /* grubu */ \t foreach (grupta var et) erişim \t { /* öğeye erişimi */ \t} } ' –