【问题标题】:Group by with multiple and nullable columns, C# Linq? [closed]用多个和可为空的列分组,C#Linq? [关闭]
【发布时间】: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 列分组: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; }
}

【问题讨论】:

  • 您的查询没问题,应该返回 2 个组。你能发布你的课程和myDataList初始化吗?
  • 好的,你可以看到编辑后的帖子
  • 但是,如果您包含作为主键的 ObjectIdDeviceId,即唯一的,那么您将获得与 w/o group by 的行数完全相同的组数.
  • 看到您的实际课程,示例已失效。 不可能告诉你需要哪一列进行分组。我可以指向任意列并告诉您将其包含在分组中。如果您想获得任何有用的意见,请发送SSCCE

标签: c# linq group-by


【解决方案1】:

为此,您还必须在 Col5 上进行分组,您可能错过了:

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

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

它会根据需要返回结果:

【讨论】:

  • 感谢您的快速回复,但这是我输入错误,请参阅编辑后的帖子
  • 你是如何展示它们的?
  • 我将它们显示为:foreach (var group in groupedItems) { /* access to group */ foreach (var item in group) { /* access to item */ } }
  • 我已经运行了你的代码,它正在根据需要返回结果
猜你喜欢
  • 2023-03-19
  • 2023-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多