在强类型LINQ中填充列表到实体查询

时间:2016-02-16 01:50:40

标签: c# entity-framework linq

我对LINQ相对较新,我只想尝试在LINQ to Entities查询中填充列表。非常感谢任何帮助。

班级" SearchCriteria"看起来像这样:

public class SearchCriteria
{
   public IList<DTOEventType1> eventTypes { get; set; }
   public IList<DTOLocation1> locs { get; set; }
   [DataType(DataType.Date)]
   [DisplayFormat(ApplyFormatInEditMode = true,
      DataFormatString="0:dd/MM/yyyy}")]
   public DateTime? searchDate { get; set; }
   public List<PortfolioLibraryMVC4V2.Domain.DTO.DTOCalendarEvents> 
      CalendarEvents;
}

我的查询如下所示:

var query = from E in medRepo.evt
    join L in medRepo.loc on E.LocationID equals L.LocationID
    join ET in medRepo.evtType on E.EventTypeID equals    
               ET.EventTypeID 
    where IDsOfSelectedEventTypes.Contains(E.EventTypeID) &&
          IDsOfSelectedLocations.Contains(L.LocationID) && 
          E.EventStart > eventslocs.searchDate 

    select new SearchCriteria
    {
        CalendarEvents = query.Select(x => new DTOCalendarEvents
        {
            Name = ET.Name,
            EventStart = E.EventStart,
            EventEnd = E.EventEnd
            }).ToList()
        };

        var datalist = query.ToList();

我试图填充列表&#34; CalendarEvents&#34;在上面的查询中选择类SearchCriteria。目前,我收到错误消息&#34;尝试在声明之前使用局部变量查询&#34;显然我的语法不正确。有人能告诉我这样做的正确方法吗?

谢谢,

皮特

2 个答案:

答案 0 :(得分:0)

您可以在linq查询之外使用SearchCriteria ..

var searchCriteria = new SearchCriteria();

searchCriteria.CalendarEvents = (from E in medRepo.evt
                                 join L in medRepo.loc 
                                 on E.LocationID equals L.LocationID
                                 join ET in medRepo.evtType 
                                 on E.EventTypeID equals ET.EventTypeID 
                                 where                     
                                 IDsOfSelectedEventTypes.Contains(E.EventTypeID) 
                                 && IDsOfSelectedLocations.Contains(L.LocationID) 
                                 && E.EventStart > eventslocs.searchDate)
                                .Select(x => new DTOCalendarEvents
                                {
                                 Name = ET.Name,
                                 EventStart = E.EventStart,
                                 EventEnd = E.EventEnd
                                })
                               .ToList();

基本上我们只需要LINQ查询中的事件列表。

答案 1 :(得分:0)

这是一个尝试:

var query = from E in medRepo.evt
    join L in medRepo.loc on E.LocationID equals L.LocationID
    join ET in medRepo.evtType on E.EventTypeID equals    
               ET.EventTypeID 
    where IDsOfSelectedEventTypes.Contains(E.EventTypeID) &&
          IDsOfSelectedLocations.Contains(L.LocationID) && 
          E.EventStart > eventslocs.searchDate 
    select new DTOCalendarEvents
    {
            Name = ET.Name,
            EventStart = E.EventStart,
            EventEnd = E.EventEnd
    };

    var datalist = query.ToList();

var searchCriteria = new SearchCriteria();
searchCriteria.CalendarOfEvents = datalist;