使用 linq 和 C# 返回与一天相关的数据而不重复

Returning data related to a day without duplication using linq and C#

下面的查询 returns 下面的示例数据。有好几天。我希望能够显示日期 与每一天的相关数据。我的预期结果也可以在下面找到。不知何故,我认为我需要 使用嵌套的 foreach 语句,但一直无法弄清楚。我是 Linq 的新手。请协助,如果你 有什么想法。

            var pracResult = from t in queryResultFilter
                                             orderby t.Day
                                             select t;
                            Int32 okeke = 0;

                            foreach (MyPlanDto chk in pracResult)
                            {
                                okeke = chk.Day;
                            }

            Sample data
            *************

            Day = Monday
            CustomerNumber = 1001
            Product = Dress

            Day = Tuesday
            customerNumber = 1002
            Product = Boxers

            Day = Wednesday
            CustomerNumber = 1003
            Product = Pencil

            Day = Monday
            CustomerNumber = 1006
            Product = Pen

            Day = Monday
            CustomerNumber = 1007
            Product = Book

            Day = Tuesday
            CustomerNumber = 1008
            Product = Erazer

            Result:
            ********

            Monday
            ********
            CustomerNumber = 1001
            Product = Dress

            CustomerNumber = 1006
            Product = Pen

            CustomerNumber = 1007
            Product = Book

            Tuesday
            ********
            customerNumber = 1002
            Product = Boxers

            CustomerNumber = 1008
            Product = Erazer

            Wednesday
            ***********
            CustomerNumber = 1003
            Product = Pencil

这是我试过但没有用的例子。

        using System;
        using System.Collections.Generic;
        using System.Linq;

        namespace MasterData
        {
            class Program
            {
                static void Main(string[] args)
                {

                    var queryResultFilter = ObjMyPlan();



                    var pracResult = (from t in queryResultFilter
                        orderby t.Day
                        select t).ToLookup(p => p.Day).Select(col1 => col1.First());

                    var pracResult2 = (from t in queryResultFilter
                                      orderby t.Day
                                      select t);


                    foreach (MyPlanDto chk in pracResult)
                    {

                        Console.WriteLine(chk.Day);

                        foreach (MyPlanDto obj1 in pracResult2)
                        {
                            var yoyou = (from t in pracResult2
                                           select t).Where(p=>p.Day.Equals(chk.Day));
                            Console.WriteLine("Customer Number:" + obj1.CustomerNumber + "Product:" + obj1.Product);
                        }

                    }
                    Console.ReadKey();

                }


                static List<MyPlanDto> ObjMyPlan()
                {
                    List<MyPlanDto> objResult = new List<MyPlanDto>();
                    objResult.Add(new MyPlanDto(){Day = "Monday", CustomerNumber = "1001", Product = "Dress"});
                    objResult.Add(new MyPlanDto(){Day = "Tuesday", CustomerNumber = "1002", Product = "Boxers"});
                    objResult.Add(new MyPlanDto(){Day = "Wednesday", CustomerNumber = "1003", Product = "Pencil"});
                    objResult.Add(new MyPlanDto(){Day = "Monday", CustomerNumber = "1006", Product = "Pen"});
                    objResult.Add(new MyPlanDto(){Day = "Monday", CustomerNumber = "1007", Product = "Book"});
                    objResult.Add(new MyPlanDto(){Day = "Tuesday", CustomerNumber = "1008", Product = "Erazer"});

                    return objResult;
                }

            }
        }

您已接近 ToLookup 方法。您应该能够使用这样的查询:

var query = queryResultFilter.OrderBy(t => t.Day)
                             .ToLookup(t => t.Day);

foreach (var group in query) {
    Console.WriteLine(group.Key);
    foreach (var item in group) {
        Console.WriteLine("CustomerNumber: {0}", item.CustomerNumber);
        Console.WriteLine("Product: {0}", item.Product);
    }
    Console.WriteLine();
}

或者,通常第一个想到的选项是使用分组,与 ToLookup 相比,它提供延迟执行。这些查询中的任何一个都可以工作(并且您可以使用上面相同的 foreach 循环):

var query = queryResultFilter.OrderBy(t => t.Day)
                             .GroupBy(t => t.Day);

// query syntax
var query = from t in queryResultFilter
            orderby t.Day
            group t by t.Day into grouped
            select grouped;

如果您想了解 ToLookupGroupBy 之间的区别,请查看 this post

请注意,按 t.Day 排序是按字母顺序排列的,那么它真的能达到您想要的目的吗?如果您的数据中有 "Friday",它会在订购时显示在 "Wednesday" 之前。如果你有日期,如果你真的需要订购结果,那会更好。