简体   繁体   English

groupby时不能隐式转换类型

[英]cannot implicitly convert type when groupby

I get this error when I try to group by CellID : 当我尝试按CellID分组时出现此错误:

Cannot implicitly convert type 'System.Collections.Generic.List System.Linq.IGrouping int,p2pControllerLogAnalyser.Models.GSMData' to 'System.Collections.Generic.List p2pControllerLogAnalyser.Models.GSMData' 无法将类型'System.Collections.Generic.List System.Linq.IGrouping int,p2pControllerLogAnalyser.Models.GSMData'隐式转换为'System.Collections.Generic.List p2pControllerLogAnalyser.Models.GSMData'

public List<GSMData> GetCellID()
{
    return Gsmdata.GroupBy(x => x.CellID).ToList();
}

What am I doing wrong and how can I fix it? 我在做什么错,我该如何解决?

If you really need to do this, though I can't imagine why, you'll need to flatten the per-group enumerables into a single list using SelectMany : 如果您确实需要这样做,尽管我无法想象为什么,您将需要使用SelectMany将每个组的可枚举对象放到一个列表中:

public List<GSMData> GetCellID()
{
    return Gsmdata
        .GroupBy(x => x.CellID)
        .SelectMany(gr => gr)
        .ToList();
}

Of course, this looks like you are trying to batch items with the same CellID together, so you could always simply order it: 当然,这似乎是您要尝试将具有相同CellID项目一起批处理,因此您始终可以简单地订购它:

public List<GSMData> GetCellID()
{
    return Gsmdata
        .OrderBy(x => x.CellID)
        .ToList();
}


Further to your comments, distinct CellID values can be returned thus: 根据您的评论,可以返回不同的CellID值,从而:

 return Gsmdata.Select(x => x.CellID).Distinct(); 

If you wish to return an ID and a count of grouped data, you can bundle that into an anonymous type: 如果您希望返回ID和分组数据的计数,可以将其捆绑为匿名类型:

 return Gsmdata .GroupBy(x => x.CellID) .Select(gr => new { CellID = gr.Key, Count = gr.Count() }); 

Though if you are returning this from a method I'd make a discoverable type and not use an anonymous type. 虽然如果您要从方法中返回此值,我会创建一个可发现的类型,而不使用匿名类型。

The problem is that GroupBy returns a grouped collection, in this case System.Collections.Generic.List<System.Linq.IGrouping<int, p2pControllerLogAnalyser.Models.GSMData>> . 问题在于GroupBy返回一个分组的集合,在这种情况下为System.Collections.Generic.List<System.Linq.IGrouping<int, p2pControllerLogAnalyser.Models.GSMData>> Your method returns a List<GSMData> . 您的方法返回List<GSMData>

In order to fix this, you need to adjust your method declaration and/or your Linq query so that the types match. 为了解决此问题,您需要调整方法声明和/或Linq查询,以使类型匹配。

From your comments, I understand that you want to return a list of the distinct cell ids. 根据您的评论,我知道您想返回不同单元格ID的列表。 You can do this by changing both the method declaration and the Linq query (I'm assuming that CellID is of type int based upon the type in the IGrouping): 您可以通过更改方法声明和Linq查询来完成此操作(我假设CellID基于IGrouping中的类型为int类型):

public List<int> GetCellID()
{
    return Gsmdata.Select(x => x.CellID)
                  .Distinct().ToList();
}

if i understand right you need something like this 如果我理解正确,您需要这样的东西

var result = (from gdata in Gsmdata
              group gdata by gbata.CellID into g
              select new Result{
                  CellID = g.Key,
                  Meters = g.Sum(i=>i.Meter)
              }
             ).ToList();

where Result is Result在哪里

public class Result{
    public /*type your CellID */ CellID;
    public /*type your Meter */  Meters;
}

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

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