[英]Populating a List in a Strongly Typed LINQ to Entities Query
I am relatively new to LINQ
and I'm simply trying to populate a List in a LINQ to Entities
query. 我是
LINQ
新手,我只是想在LINQ to Entities
查询中填充一个List。 Any assistance would be greatly appreciated. 任何帮助将不胜感激。
The class "SearchCriteria" looks like this: 类“ 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;
}
and my query is shown below: 我的查询如下所示:
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();
I'm trying to populate the list "CalendarEvents" in the Class SearchCriteria in the select in the above query. 我正在尝试在上述查询的select中填充Class SearchCriteria中的列表“ CalendarEvents”。 Currently, I get the error message "trying to use the local variable query before it is declared" so obviously my syntax isn't correct.
当前,我收到错误消息“在声明之前尝试使用局部变量查询”,因此显然我的语法不正确。 Can someone show me the proper way to do this?
有人可以告诉我正确的方法吗?
Thanks, 谢谢,
Pete 皮特
You can take SearchCriteria
outside of the linq query.. 您可以在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();
basically we only need the list of events from the LINQ query. 基本上,我们只需要LINQ查询中的事件列表。
Give this is a try: 试试看:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.