none
LINQ group by: How to get those field value which are not in group by clause RRS feed

  • Question

  • Tell me easy way get those field value which are not in group by clause using LINQ. here i got the code

    var list = new List<Child>()
                    {
                        new Child()
                            {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "John"},
                        new Child()
                            {School = "School2", FavoriteColor = "blue", Friend = "Bob", Name = "Pete"},
                        new Child()
                            {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "Fred"},
                        new Child()
                            {School = "School2", FavoriteColor = "blue", Friend = "Fred", Name = "Bob"},
                    };
                    
    var newList = list.GroupBy(x => new {x.School, x.Friend, x.FavoriteColor})
                        .Select(y => new ConsolidatedChild()
                                            {
                                                FavoriteColor = y.Key.FavoriteColor,
                                                Friend = y.Key.Friend,
                                                School = y.Key.School,
                                                Children = y.ToList()
                                            }
                        );
                        
    foreach(var item in newList)
    {
        Console.WriteLine("School: {0} FavouriteColor: {1} Friend: {2}", item.School,item.FavoriteColor,item.Friend);
        foreach(var child in item.Children)
        {
            Console.WriteLine("\t Name: {0}", child.Name);
        }
    }                    
    without for loop how could i show each group name field's value. please post a sample code. thanks

    Wednesday, October 9, 2019 4:09 PM

Answers

  • Hi Sudip_inn,

    Thanks for your feedback.

    You can use while loop instead of foreach loop.

                var newList = list.GroupBy(x => new { x.School, x.Friend, x.FavoriteColor })
                                    .Select(y => new ConsolidatedChild()
                                    {
                                        FavoriteColor = y.Key.FavoriteColor,
                                        Friend = y.Key.Friend,
                                        School = y.Key.School,
                                        Children = y.ToList()
                                    }).ToArray<ConsolidatedChild>();
                int i = 0;
                while (i< newList.Length)
                {
                    Console.WriteLine($"FavoriteColor: {newList[i].FavoriteColor} Friend: {newList[i].Friend} School: {newList[i].School}");
                    i++;
                }

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, October 17, 2019 9:38 AM
    Wednesday, October 16, 2019 9:51 AM
    Moderator

All replies

  • I don't understand your question.  The code seems to be doing exactly what you ask.  Your newList will contain two records.  You can't show the individual fields without iterating through them somehow.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, October 9, 2019 10:04 PM
  • Hi Sudip_inn, 

    Thank you for posting here.

    According to your description, you want to get values which are not in group by clause.

    I make a test on my side, and you can refer to the following code:

        class Program
        {
            static void Main(string[] args)
            {
                var list = new List<Child>()
                    {
                        new Child()
                            {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "John"},
                        new Child()
                            {School = "School2", FavoriteColor = "blue", Friend = "Bob", Name = "Pete"},
                        new Child()
                            {School = "School1", FavoriteColor = "blue", Friend = "Bob", Name = "Fred"},
                        new Child()
                            {School = "School2", FavoriteColor = "blue", Friend = "Fred", Name = "Bob"},
                    };
    
                var newList = list.GroupBy(x => new { x.School, x.Friend, x.FavoriteColor });
    
                List<ConsolidatedChild> lst = new List<ConsolidatedChild>();
                foreach (var childGroup in newList)
                {
                    ConsolidatedChild consolidatedChild = new ConsolidatedChild() {
                        FavoriteColor = childGroup.Key.FavoriteColor,
                        Friend = childGroup.Key.Friend,
                        School = childGroup.Key.School,
                        Children = childGroup.ToList()
                    };
                    lst.Add(consolidatedChild);
                }
    
                foreach (var result in lst)
                {
                    Console.WriteLine($"FavoriteColor: {result.FavoriteColor} Friend: {result.Friend} School: {result.School}");
                    foreach (var child in result.Children)
                    {
                        Console.Write("Children:");
                        Console.WriteLine($"Children's Name: {child.Name}");
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        }
        class Child
        {
            public string School { get; set; }
            public string FavoriteColor { get; set; }
            public string Friend { get; set; }
            public string Name { get; set; }
        }
        class ConsolidatedChild
        {
            public string School { get; set; }
            public string FavoriteColor { get; set; }
            public string Friend { get; set; }
            public List<Child> Children { get; set; }
        }

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 11, 2019 3:16 AM
    Moderator
  • i do not want to use foreeach loop to get value for those fields which are not in group by clause. so what to change in code ?
    Friday, October 11, 2019 8:56 AM
  • Hi Sudip_inn,

    Thanks for your feedback.

    You can use while loop instead of foreach loop.

                var newList = list.GroupBy(x => new { x.School, x.Friend, x.FavoriteColor })
                                    .Select(y => new ConsolidatedChild()
                                    {
                                        FavoriteColor = y.Key.FavoriteColor,
                                        Friend = y.Key.Friend,
                                        School = y.Key.School,
                                        Children = y.ToList()
                                    }).ToArray<ConsolidatedChild>();
                int i = 0;
                while (i< newList.Length)
                {
                    Console.WriteLine($"FavoriteColor: {newList[i].FavoriteColor} Friend: {newList[i].Friend} School: {newList[i].School}");
                    i++;
                }

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, October 17, 2019 9:38 AM
    Wednesday, October 16, 2019 9:51 AM
    Moderator