2016-03-25 36 views
-2

我在C#中的列表如下所示:由多个可空列组成,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个项目:

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

实际类group by使用的是实体框架视图,我写了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

您所查询的是确定的,并且应该返回2组。你可以发布你的类和'myDataList'初始化吗? –

+0

好的,你可以看到编辑后的文章 –

+0

但是,如果你包含作为主键的唯一的ObjectId和DeviceId,那么你将得到与没有组的行数完全相同的组数通过。 –

回答

1

你要组上Col5以及对于这一点,你可能错过了:

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

现在您可以对结果进行迭代,以使用Keys和Group against Group进行打印或操作。

并根据需要返回的结果:

enter image description here

+0

感谢您的快速响应,但这是我在打字时的错误,请参阅编辑后的文章 –

+0

您如何显示它们? –

+0

我显示它们为以下: '的foreach(在groupedItems VAR组) { /*访问组*/ \t的foreach(在组VAR项) \t { /*访问项*/ \t} }' –