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:
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:
public List<GSMData> GetCellID()
{
return Gsmdata
.OrderBy(x => x.CellID)
.ToList();
}
CellIDreturn 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:
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.
GroupByreturns anIEnumerable<IGrouping<TKey, TElement>>, so it cannot be translated toList<TElement>in the manner you are doing. Why are you grouping at all? Why notOrderByinstead? Grouping and then immediately trying to flatten a list is either a) unintended or b) mimicking ordering semantics. - Adam Houldsworth