0

I'm trying to convert a List of MyObject that I'm doing operation on by using LINQ and turning back to a list of MyObject.

public class Part
{
    public string ID { get; set; }
    public string DescFr { get; set; }
    public string DescEng { get; set; }
    public string PartNumber{ get; set; }
    public Part(string strID, string strDescFr, string strDescEng, string strPartNumber) 
    {
        this.ID = strID;
        this.DescFr = strDescfr;
        this.DescEng = strDescEng;
        this.PartNumber= strPartNumber;
    }
}

So in another class I use that class to create a list of Part objects via the database.

List<Part> lstParts = DB.Query.GetParts();

After that I'm grouping them by description

var lstGrouped = lstParts.GroupBy(x => x.DescrFr);

EDIT:

var lstGrouped = lstParts.GroupBy(x => x.PartNumber);

How can I convert back the lstGrouped back to List<Part>? I have tried the ToList() method and cast it to List<Part>, it does not work, giving me an cast error:

enable to cast object of type

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
Sam
  • 85
  • 3
  • 16
  • Why are you grouping, only to go back to an ungrouped collection? – BradleyDotNET May 12 '15 at 23:28
  • Well this was only an exemple, but I'm grouping on my PartNumber which can be duplicates. I'll Edit the it – Sam May 12 '15 at 23:29
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 12 '15 at 23:30
  • Not really I just need to show the single PartNumber not douplicates, I know I could group by in the quey but I need all the parts for other instructions, which is why I'm trying to group by using linq, – Sam May 12 '15 at 23:34
  • This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/); please tell us what result you are trying to get, why you think “convert back the lstGrouped back to `List`” is going to do that, and why you would do it in your code instead of whatever `GetParts` calls. Also please post your real code and the real error message; making things up makes it impossible to guess what you are trying to accomplish. – Dour High Arch May 12 '15 at 23:40

3 Answers3

1

Since grouping returns an IEnumerable of IGrouping you can't just transform it back into the sub-list. If you are trying to remove duplicates, just select the first one:

List<Part> finalList = lstGrouped.Select(g => g.First()).ToList();

If you want all the items back (at which point, why did you group them?) then use SelectMany

List<Part> finalList = lstGrouped.SelectMany(g => g).ToList();
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
0

GroupBy returns an IEnumerable<IGrouping<TKey, TSource>>so you are no longer working w/ just a list of TSource (in your case Part), but a list of groupings.

You may want to iterate over the groupings and get the list from each (see How to get values from IGrouping).

Hard to fully answer since I'm not clear on objective (why you would group, just to get the ungrouped list again).

Community
  • 1
  • 1
Christoph
  • 4,251
  • 3
  • 24
  • 38
0

Looks like you want to get distinct items from the list of items. In that case you can either use Distinct method on the original list or you can go with your group approach and do the following -

lstParts.GroupBy(x => x.PartNumber).Select(x=>x.First()).ToList();

Sanjay Singh
  • 714
  • 1
  • 8
  • 15