locked
how to group by enum value

    Question

  • Hey guys, I've been looking around SO and other places, but I'm a bit confused as to how to go about this. I have an enum:
    public enum MetabolizationRate
        {
            Normal = 0,
            Impaired = 1,
            ElevatedRisk = 2
        }

    and I want to group some object by the enum value, something like:
    var result = ReportViewModel.ReportModel.GroupItems;
                var grouped = result.GroupBy(a => a.MetabolicRate);

    But it doesn't seem to be grouping properly. I've tried assigning strings to the model and other things, but I can't seem to get the result that I'm looking for. Thanks a bunch for the help!

    Michael DiLeo

    Saturday, July 19, 2014 5:50 PM

Answers

  • This is a Lambda Expression problem, nothing to do with Windows Store Apps. Anyway assuming following data structure

    public enum MetabolizationRate
    {
        Normal = 0,
        Impaired = 1,
        ElevatedRisk = 2
    }
    
    public class Item
    {
        public MetabolizationRate MetabolizationRate { get; set; }
        public int Field1 { get; set; }
        public string Field2 { get; set; }
        public Object Field3 { get; set; }
    }

    You can  use following code to do grouping

    public void GroupData()
    {
        List<Item> items = new List<Item>();
    
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 1", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 2", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 3", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 4", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 5", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 6", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 7", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Normal, Field1 = 1, Field2 = "Test 8", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 9", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 10", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 11", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 12", Field3 = null });
    
        //By Lambda
        var groupedItems = (items.GroupBy(i => i.MetabolizationRate)
            .Select(i => new
            {
                MetabolizationRate = i.Key,
                Items = i.Select(x => x).ToList()
            })).ToList();
    
        //By LINQ 
        var groupedItems2 = (from i in items 
                            group i by i.MetabolizationRate into g
                            select new 
                            { 
                                MetabolizationRate = g.Key,
                                Items = (from x in g select x).ToList()
                            }).ToList();
    }



    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!


    Saturday, July 19, 2014 6:58 PM

All replies

  • This is a Lambda Expression problem, nothing to do with Windows Store Apps. Anyway assuming following data structure

    public enum MetabolizationRate
    {
        Normal = 0,
        Impaired = 1,
        ElevatedRisk = 2
    }
    
    public class Item
    {
        public MetabolizationRate MetabolizationRate { get; set; }
        public int Field1 { get; set; }
        public string Field2 { get; set; }
        public Object Field3 { get; set; }
    }

    You can  use following code to do grouping

    public void GroupData()
    {
        List<Item> items = new List<Item>();
    
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 1", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 2", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 3", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 4", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 5", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 6", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 7", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Normal, Field1 = 1, Field2 = "Test 8", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 9", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.Impaired, Field1 = 1, Field2 = "Test 10", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 11", Field3 = null });
        items.Add(new Item() { MetabolizationRate = MetabolizationRate.ElevatedRisk, Field1 = 1, Field2 = "Test 12", Field3 = null });
    
        //By Lambda
        var groupedItems = (items.GroupBy(i => i.MetabolizationRate)
            .Select(i => new
            {
                MetabolizationRate = i.Key,
                Items = i.Select(x => x).ToList()
            })).ToList();
    
        //By LINQ 
        var groupedItems2 = (from i in items 
                            group i by i.MetabolizationRate into g
                            select new 
                            { 
                                MetabolizationRate = g.Key,
                                Items = (from x in g select x).ToList()
                            }).ToList();
    }



    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you. Happy Coding!!!


    Saturday, July 19, 2014 6:58 PM
  • Thank so much for such a detailed reply! You are so awesome!
    I also apologize for being so late to thank you for your help; it's very rude of me.

    Michael DiLeo

    Monday, August 4, 2014 1:57 AM