[英]How do i get this conditional include projection to instantiate the “premis” entity-object property of the ref_calendar_premis entity-object?
I'm working on my first EntityFramework-solution, and I'm stumped. 我正在研究我的第一个EntityFramework解决方案,但我很困惑。
When I'm iterating through the resulting "calendar"-entity objects, the property calendar.ref_calendar_premisis.premisis
is un-instantiated ( null
). 当我遍历生成的“ calendar”实体对象时,未实例化calendar.ref_calendar_premisis.premisis
属性( null
)。 I understand why, I deactivated lazy-loading because i wanted to control the conditional "includes" in a syntax-tree projection like so: 我了解为什么,我停用了延迟加载,因为我想像这样在语法树投影中控制条件“包含”:
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;
}
I tried adding something like: 我尝试添加类似的内容:
...
room = c.ref_calendar_premises.Select(r => r.premis),
...
... But to no avail. ...但是无济于事。 A room has been booked for the "Dinner with Allan" event in our test data, but i cant seem to get it to load the premis-entyties. 在我们的测试数据中已经为“与Allan一起吃饭”事件预订了一个房间,但我似乎无法容纳这个房间。
I have no previous experience with EntityFramework, LINQ to SQL or any other ORM's, so I might be missing something plainly obvious. 我以前没有使用EntityFramework,LINQ to SQL或任何其他ORM的经验,因此我可能会缺少明显显而易见的内容。
Any suggestions? 有什么建议么?
I twisted the arm of our DB-responsible, and turns out premisisid is null for all rows because of a conversion error (i was looking at testdata documentation, not the actual data). 我扭曲了数据库负责人的手臂,结果由于转换错误,所有行的premisisid为null(我在看testdata文档,而不是实际数据)。 So thanks for the input and your time, I'll just show myself out 0_0 因此,感谢您的投入和宝贵的时间,我将向自己展示0_0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.