简体   繁体   English

使用LINQ对数据进行分组不起作用

[英]Grouping Data with LINQ Not Working

I want to query a table and group by field BillPeriodId and I'm implementing it in the way the example in this article was done. 我想按字段BillPeriodId查询表和组,我正在按照本文中的示例的方式实现它。 Below is the method doing the grouping 以下是进行分组的方法

public List<BillPeriodLine> GetLinesGroupedByPeriod()
{
    var linesGrouped = _lineRepository.AllIncluding(p => p.BillPeriod)
                                          .GroupBy(pl => pl.BillPeriodId)
                                          .Select(g => new Group<int, BillPeriodLine>
                                          {
                                              Key = g.Key,
                                              Values = g
                                          });
    return new List<BillPeriodLine>(linesGrouped);
}

The Discoverable Type 可发现类型

public class Group<T, K>
{
    public K Key;
    public IEnumerable<T> Values;
}

But it underlines g.Key with the following error message: 但它强调了g.Key并出现以下错误消息:

Cannot implicitly convert type 'int' to 'BillTracker.Entities.BillPeriodLine' 无法将类型'int'隐式转换为'BillTracker.Entities.BillPeriodLine'

It's exactly the way it's done in the example except I'm using lamda expressions. 除了我正在使用lamda表达式之外,它正是在示例中完成的方式。 It gives same error using LINQ statements. 它使用LINQ语句提供相同的错误。 I don't know what I'm doing wrong. 我不知道我做错了什么。 Sompne point me to the right direction please? Sompne指向正确的方向吗?

you have T and K around the wrong way (or vice versa ) 你有错误的TK反之亦然

new Group<int, BillPeriodLine>

... ...

public class Group<T, K>
{
    public K Key;
    public IEnumerable<T> Values;
}

Cannot implicitly convert type 'int' to 'BillTracker.Entities.BillPeriodLine' 无法将类型'int'隐式转换为'BillTracker.Entities.BillPeriodLine'

The message says it perfectly, you are trying to push a square peg in a round hole 信息说得很完美,你试图在一个圆孔中推一个方形钉

While Michael Randall's answer is perfectly sound regarding the error message, this is only part of the issue. 虽然Michael Randall对错误消息的回答非常合理,但这只是问题的一部分。

I guess that you'd like to display the grouped BillPeriodLine s in some sort of table as shown in the link you provided. 我想你想在某种表格中显示分组的BillPeriodLine ,如你提供的链接所示。 Anyway, you can't simply return a List<BillPeriodLine> and assume it to be grouped magically, neither can you create a List<BillPeriodLine> from an IEnumerable<Group<int, BillPeriodLine>> - which is the type of linesGrouped . 无论如何,你不能简单地返回List<BillPeriodLine>并假设它被神奇地分组,你也不能从IEnumerable<Group<int, BillPeriodLine>>创建List<BillPeriodLine> - 这是linesGrouped的类型。 Presumably what you were trying to do, is to return a List<Group<int, BillPeriod>> 大概你要做的是返回List<Group<int, BillPeriod>>

public List<Group<int, BillPeriodLine>> GetLinesGroupedByPeriod()
{
    var linesGrouped = _lineRepository.AllIncluding(p => p.BillPeriod)
                                      .GroupBy(pl => pl.BillPeriodId)
                                      .Select(g => new Group<int, BillPeriodLine>
                                      {
                                          Key = g.Key,
                                          Values = g
                                      });
    return linesGrouped.ToList();
}

This List<Group<int, BillPeriodLine>> can be used from a view as shown in the example: The view is iterating over the List<Group<int, BillPeriodLine>> . 可以从示例中显示的视图中使用此List<Group<int, BillPeriodLine>> :视图正在迭代List<Group<int, BillPeriodLine>> For each Group<int, BillPeriodLine> it creates a header for the group and then iterates over all items in Group<int,BillPeriodLine>.Values creating a line for each BillPeriodLine . 对于每个Group<int, BillPeriodLine>它为组创建一个标题,然后遍历Group<int,BillPeriodLine>.Values所有项目,为每个BillPeriodLine创建一行。

Furthermore (as Michael stated) you'll have to swap the type parameters of your Group<T,U> class (or switch the types where you create the Group<int, BillPeriodLine> , but I'd prefer the former, since I'd consider the key coming first a bit more intuitive). 此外(正如迈克尔所说)你必须交换Group<T,U>类的类型参数(或者切换你创建Group<int, BillPeriodLine> ,但我更喜欢前者,因为我首先考虑关键是更直观一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM