具有多个和可空列的分组依据,C#Linq?
我在c#中有一个列表,如下所示:具有多个和可空列的分组依据,C#Linq?,c#,linq,group-by,C#,Linq,Group By,我在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
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();
groupby
中使用的实际类是实体框架中的一个视图,我写了7列。我想把它分为7列:
ObjectId
,DeviceSpecificationCategoryId
,DeviceSpecificationCategoryIsHidden
,DeviceSpecificationCategoryName
,devicespecificationcategorypersianame
,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; }
}
[EntityFlag]
公共部分类DevicePresentationView:BaseEntity
{
[主密钥]
public int ObjectId{get;set;}
[主密钥]
公共int设备ID{get;set;}
公共可空设备SpecificationCategoryId{get;set;}
公共可为空的设备SpecificationCategorisHidden{get;set;}
公共字符串设备SpecificationCategoryName{get;set;}
公共字符串设备SpecificationCategorypersianName{get;set;}
公共可为空的设备SpecificationCategorYorrumberInDevicePage{get;set;}
公共可为空的设备SpecificationItemId{get;set;}
公共字符串设备SpecificationItemName{get;set;}
公共字符串设备SpecificationImpersianName{get;set;}
公共可为空的设备SpecificationItemHidden{get;set;}
公共可为空的设备SpecificationOnItemerNumberInDevicePage{get;set;}
公共字符串设备SpecificationItemDescription{get;set;}
公共可为空的设备SpecificationOnItemPrimary{get;set;}
公共可为空的设备SpecificationOnItemEssential{get;set;}
公共字符串设备SpecificationItemUnitName{get;set;}
公共字符串设备SpecificationItemUnitPersianName{get;set;}
公共可空设备SpecificationItemValueTypeId{get;set;}
公共可空设备SpecificationValueId{get;set;}
公共字符串设备指定值{get;set;}
公共可空设备SpecificationNumericValue{get;set;}
公共可空设备BenchmarkCategoryId{get;set;}
公共可为空的设备BenchmarkCategoryHidden{get;set;}
公共字符串设备BenchmarkCategoryName{get;set;}
公共字符串设备benchmarkcategorypersianame{get;set;}
公共可为空的设备BenchmarkCategorOrderNumberDevicePage{get;set;}
公共可空设备BenchmarkCategoryParentID{get;set;}
公共字符串设备BenchmarkCategoryDescription{get;set;}
公共可为空的设备BenchmarkItemId{get;set;}
公共字符串设备BenchmarkItemName{get;set;}
公共可为空的设备benchmarkitemishidden{get;set;}
公共字符串设备BenchmarkItemPersianName{get;set;}
公共可为空的设备BenchmarkItemOrderNumberDevicePage{get;set;}
公共字符串设备BenchmarkItemDescription{get;set;}
公共可空设备BenchmarkItemisPrimary{get;set;}
公共字符串设备BenchmarkItemUnitName{get;set;}
公共字符串设备BenchmarkItemUnitPersianName{get;set;}
公共可为空的设备BenchmarkItemValueTypeId{get;set;}
公共可为空的设备BenchmarkValueId{get;set;}
公共字符串设备基准值{get;set;}
公共可空设备BenchmarkNumericValue{get;set;}
公共可为空的设备BenchmarkAttachmentId{get;set;}
}
您还必须在Col5
上分组,因为您可能错过了:
var groupedItems = myDataList
.GroupBy(q =>
new
{
q.Col1,
q.Col2,
q.Col3,
q.Col4,
q.Col5 // notice this
}).ToList();
现在,您可以对结果进行迭代,以打印或使用键进行操作,并对组进行分组
并根据需要返回结果:
谢谢您的快速回复,但这是我键入的错误,请参见编辑的帖子您如何显示它们?我将它们显示如下:
foreach(groupedItems中的var组){/*access to group*/foreach(组中的var项){/*access to item*/}
我已经运行了您的代码,并且它会根据需要返回结果。您的查询正常,应该返回2个组。你可以发布你的类和myDataList
初始化吗?好的,你可以看到编辑过的post,但是如果你包含ObjectId
和DeviceId
这两个主键,即unique,那么你将得到与没有分组的行数完全相同的组数。看到你的实际类,这个例子就无效了。而且不可能知道需要对哪个列进行分组。我可以指向任意一列,并告诉您将其包含在分组中。如果你想得到任何有用的输入,就做一个测试。