如何在 Linq 中检索不同的列表?

How can i retrieve a distinct list in Linq?

我想要这样的结果

McLaren Cup
       The Regis Brunch
                  21st Jan 2017
                  24st Jan 2017
                  26st Jan 2017
       General Entry
       General Parking
        Picnic Parking
                  21st Jan 2017
                  24st Jan 2017
                  26st Jan 2017

为此我做了 LINQ 查询,但是我得到如下异常 附加信息:'Distinct' 操作无法应用于指定参数的集合 ResultType。

  var Ticketdata = (from Tevent in context.TicketEventsMaster
  where Tevent.IsActive == true && Tevent.IsVisible == true
  select new
  {
    EventID = Tevent.TicketEventID,
    eventName = Tevent.TicketEventName,
    EventCategories = (from TEM in context.TicketCategoriesMaster
       join TED in context.TicketEventDates on TEM.CategoryID equals TED.TCategoryID
       where TED.IsActive == true && TED.IsVisible == true && TED.TEventID == Tevent.TicketEventID
       //orderby TED.DisplayOrder
       select new
      {
          CategoryID = TED.TCategoryID,
          CategoryName = TEM.CategoryName,
          EventDates = (from tDate in context.TicketEventDates
            where
              tDate.IsActive == true && tDate.IsVisible == true
              && tDate.TEventID == Tevent.TicketEventID
              && tDate.TCategoryID == TEM.CategoryID
            orderby tDate.TEventDate
            select new
            {
              DateID = TED.ID,
              Dates = TED.TEventDate != null ? TED.TEventDate.ToString() : string.Empty,
              Times = TED.TEventTime != null ? TED.TEventTime.ToString() : string.Empty,
            }).ToList()
      }).Distinct().ToList()

  }).ToList();

有很多方法可以获取不同的数据。 您可以使用具有所需属性的 "DistinctBy" 或者您可以使用

 var result = table.GroupBy(test => test.id)
                   .Select(grp => grp.First())
                   .ToList();