2016-03-25 13 views
-2

私は次のようにC#でのリストを持っている:複数のnull可能な列でグループ化すると、C#Linq?

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 

私は、LINQで次の結果を取得できますか?

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 

私は以下のようにそれをやったが、それは8つの項目でただ一つのグループを返します。group byで使用する

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

実際のクラスは、Entity Frameworkの中に表示され、私は7列を書きましたそれの。私は7つの最初の列でグループにそれをしたい
ObjectIdDeviceIdDeviceSpecificationCategoryIdDeviceSpecificationCategoryIsHiddenDeviceSpecificationCategoryNameDeviceSpecificationCategoryPersianNameDeviceSpecificationCategoryOrderNumberInDevicePageグループにあなたが持っている

[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

あなたのクエリがOKで、2グループを返す必要があります。あなたはあなたのクラスと 'myDataList'初期化を投稿できますか? –

+0

OK、編集済みの投稿 –

+0

を見ることができますが、プライマリキーである 'ObjectId'と' DeviceId'を含めると、一意である場合、グループに含まれる行の数とまったく同じ数のグループが得られます。 –

答えて

1

Col5上だけでなく、そのため、あなたはおそらくそれを逃した:

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

これで、キーまたはグループを使用してグループに対して印刷または操作することができます。

し、必要に応じて、それが結果を返す:

enter image description here

+0

素早く対応してくれてありがとうございましたが、入力ミスでした。編集された投稿 –

+0

をどうやって表示していますか? –

+0

Iは、次のようにそれらを表示: 'foreachの(groupedItemsにおけるVAR基) { /*グループ*/ \t foreachの(グループ内のvarアイテム)にアクセス \t { /*アイテムへのアクセス*/ \t} } ' –

関連する問題