【问题标题】:How can i retrieve a distinct list in Linq?如何在 Linq 中检索不同的列表?
【发布时间】:2017-11-13 13:51:55
【问题描述】:

我想要这样的结果

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();

【问题讨论】:

    标签: asp.net linq asp.net-mvc-4 c#-4.0 entity-framework-4


    【解决方案1】:

    有很多方法可以获取不同的数据。 您可以将“DistinctBy”与 deisred 属性一起使用,也可以使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多