1
我正在處理我的第一個EntityFramework解決方案,我很難過。如何獲取此條件包含投影來實例化ref_calendar_premis實體對象的「premis」實體 - 對象屬性?
當我遍歷生成的「日曆」實體對象時,屬性calendar.ref_calendar_premisis.premisis
未實例化(null
)。我明白爲什麼,我停用延遲加載,因爲我想控制條件「包括」在語法樹投影像這樣:
private List<CalendarBlockDTO> FillUserCalendar(DateTime start, DateTime end, int uid, bool everything)
{
var result = new List<CalendarBlockDTO>();
using (var db = new ViggoEntities())
{
//We need to disable lazyloading to use the "expression tree" syntax
db.Configuration.LazyLoadingEnabled = false;
//flip our "everything" so we can compare it to our "special" column
int specialScope = Convert.ToInt32(!everything);
//Build a query "Projection" with "expression tree" syntax
var query = from c in db.calendars
select new
{
calendarEntry = c,
createdByUser = c.MadeByUser,
premisesBookings = c.ref_calendar_premises.Where
(
rcp => rcp.deleted == 0 &&
(
//started before the start-parameter AND ended after start-parameter
(rcp.timestart < start && rcp.timeend > start) ||
//OR startet before the end-parameter AND ended after the end-parameter
(rcp.timestart < end && rcp.timeend > end) ||
//OR startet before the start-parameter AND ended after the end-paremeter
(rcp.timestart < start && rcp.timeend > end) ||
//OR startet after the start-parameter AND ended before the end-parameter
(rcp.timestart > start && rcp.timeend < end)
)
),
attendingGroups = c.ref_groups_calendar.Where
(
rug => rug.deleted == 0
),
groups = c.ref_groups_calendar.Select(rgc => rgc.usergroup),
////Assignments not implemented yet
////assignments = c.
schedules = c.ref_calendar_schedule.Where
(
sch => sch.deleted == 0
)
};
var calEntries =
query.ToArray().Select(c => c.calendarEntry).
Where(
//If only special requested, show only special
c => c.special >= specialScope &&
//If not "MadeInInfo", show for creator as well
(c.madeininfo==0 && c.made_by == uid) ||
//Else, show to involved users
(c.ref_groups_calendar.Any(rgc => rgc.usergroup.ref_users_groups.Any(rug => rug.userid == uid)))
);
foreach (var calendar in calEntries)
{
//I WANT THIS TO NOT THROW AN EXCEPTION, PREMIS SHOULD NOT BE NULL
if (calendar.name == "Dinner with Allan" && calendar.ref_calendar_premises.Any(rcp => rcp.premis == null))
throw new Exception("Premis not instantiated!");
result.AddRange(CalendarToCalendarBlockDTOs(calendar));
}
}
return result;
}
我嘗試添加類似:
...
room = c.ref_calendar_premises.Select(r => r.premis),
...
..但無濟於事。我們的測試數據中預訂了一個房間,用於「與艾倫一起吃晚餐」活動,但我似乎無法獲得它加載premis-entyties。我沒有以前的EntityFramework,LINQ to SQL或任何其他ORM的經驗,所以我可能會漏掉一些明顯的東西。
有什麼建議嗎?
'premis'是一種集合類型嗎? – Slauma
不,是實體類「premisis」的對象 – Julian
有趣的是,你說「包括」但不使用'c.ref_calendar_premises.Include(r => r.premis)'。 –