我们有一个名为rowData的Page类集合。我需要通过名为PartitionKey
的列的子字符串对这些类进行分组。
var a = from Page in rowData
group Page by new {
Page.PartitionKey.Substring(0,2),
Page.PartitionKey.Substring(2,6),
Page.PartitionKey.Substring(8)
} into group
select new {
group.key.SubjectId
, group.key.bookId
, group.Key.chapterId
, total = rowData.sum(s => s.Page)
};
我们尝试了这个建议,但它给出了一个错误:
Error 1 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
有谁知道我们做错了什么?
这需要为报告创建数据。数据应显示每个主题的书籍,章节和页面的数量。我们的记录如下:
Primary key (columns 0-1 show the subject)
Primary key (columns 2-6 show the book ID)
Primary key (columns 8-12 show the chapter ID)
Row Key < there is a unique key for each row
因此,主键和行的组合是唯一的,代表一个页面。
以下是主键的示例:
010000100001
010000100001
010000100001
010000100002
010000100002
010000100002
010000200003
010000200003
020000300004
020000300005
在此示例中,我们需要一个类似于以下内容的报告:
Subject Books Chapters Pages
01 2 3 8
02 1 2 2
答案 0 :(得分:3)
好的,现在你已经澄清了这个问题,我相信你实际只想按主题分组。我怀疑你想要的东西:
var a = from page in rowData
select new {
SubjectId = Page.PartitionKey.Substring(0,2),
BookId = Page.PartitionKey.Substring(2,6),
ChapterId = Page.PartitionKey.Substring(8)
} into split
group split by split.SubjectId into g
select new {
SubjectId = g.Key,
Books = g.GroupBy(x => x.BookId).Count(),
Chapters = g.GroupBy(x => x.ChapterId).Count(),
Pages = g.Count()
};
看起来不错吗?
原始回答
您需要以匿名类型指定属性名称:
var a = from page in rowData
group page by new {
SubjectId = Page.PartitionKey.Substring(0,2),
BookId = Page.PartitionKey.Substring(2,6),
ChapterId = Page.PartitionKey.Substring(8)
} into g
select new {
g.Key.SubjectId,
g.Key.BookId,
g.Key.ChapterId,
Total = g.Sum(s => s.PageNumber)
};
(你需要检查Sum
论点 - 你不清楚你在那里尝试做什么。)
答案 1 :(得分:2)
您的组密钥需要成员名称:
var a = from Page in rowData
group Page by new {
S1 = Page.PartitionKey.Substring(0,2),
S2 = Page.PartitionKey.Substring(2,6),
S3 = Page.PartitionKey.Substring(8)
} into group
select new {
group.key.S1
, group.key.S2
, group.Key.S3
, total = rowData.sum(s => s.Page)
};
我不会在此处使用group
作为名称,最好将其称为into g
或其他内容。