Dr.YSG
Dr.YSG

Reputation: 7591

Linq sorting, grouping, and returning a dictionary

I have an array of custom objects (PONO) called FrameList, I can orderBy and GroupBy to sort and group. But now I want to return a dictionary, and I am getting the error:

(BTW, there is no error is I remove the .ToDictionary() method. )

Error 1 'System.Linq.IGrouping' does not contain a definition for 'type' and no extension method 'type' accepting a first argument of type 'System.Linq.IGrouping' could be found (are you missing a using directive or an assembly reference?) C:\Users\ysg4206\Documents\Visual Studio 2010\Projects\Watson\CatalogServiceClasses\BuildToby.cs 21 38 Watson

Here is the snippet that sorts and groups:

    var dict = request.FrameList.OrderBy(f => f.trueDate)
        .GroupBy(f => f.type)
        .ToDictionary(f => f.type, f => f);

and here is the definition of the FrameList (which is a simple array of FrameData)

[DataContract]
public class FrameData
{
    [DataMember]
    public String idx { get; set; }
    [DataMember]
    public String type { get; set; }
    [DataMember]
    public String path { get; set; }
    [DataMember]
    public String date { get; set; }
    [DataMember]
    public DateTime trueDate { get; set; }
}

Upvotes: 1

Views: 3569

Answers (3)

Ani
Ani

Reputation: 113442

It's not clear what you're trying to do.

If you're just looking to create an idiomatic multi-map, try:

var lookup = request.FrameList.ToLookup(f => f.type);

If you're looking to create a multi-map with the Dictionary class, try:

var dict = request.FrameList
                  .GroupBy(f => f.type)
                  .ToDictionary(g => g.Key, g => g.ToList());

Do note that in both of these cases, OrderBy before grouping is useless since Lookup and Dictionary are inherently unordered collections. If you're trying to accomplish something with your ordering (perhaps you want items within a group to be ordered by date?), please clarify, and we might be able to provide something more appropriate.

EDIT: Based on your comment, looks like you need:

var dict = request.FrameList
                  .GroupBy(f => f.type)
                  .ToDictionary(g => g.Key, 
                                g => g.OrderBy(f => f.trueDate)
                                      .ToList());

Upvotes: 4

Ilya Ivanov
Ilya Ivanov

Reputation: 23636

Assuming your method signature is IDictionary<string,IList<FrameData>>

Try to use next code snippet:

var dict = request.FrameList
    .OrderBy(f => f.trueDate)
    .GroupBy(f => f.type)
    .ToDictionary(f => f.Key, f => f.ToList());

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236268

GroupBy operator returns IGrouping objects. Those objects have Key property. But there is no type property on grouping object (its property of your entity):

 var dict = request.FrameList
        .OrderBy(f => f.trueDate)
        .GroupBy(f => f.type) // this selects IEnumerable<IGrouping>
        .ToDictionary(g => g.Key, g => g);

NOTE: If this is a Linq to Entities query, then GroupBy returns IQueryable<IGrouping<TKey, TElement>>

Upvotes: 1

Related Questions