【发布时间】:2016-03-25 14:13:23
【问题描述】:
我在 c# 中有一个如下列表:
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 列分组: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; }
}
【问题讨论】:
-
您的查询没问题,应该返回 2 个组。你能发布你的课程和
myDataList初始化吗? -
好的,你可以看到编辑后的帖子
-
但是,如果您包含作为主键的
ObjectId和DeviceId,即唯一的,那么您将获得与 w/o group by 的行数完全相同的组数. -
看到您的实际课程,示例已失效。 不可能告诉你需要哪一列进行分组。我可以指向任意列并告诉您将其包含在分组中。如果您想获得任何有用的意见,请发送SSCCE。