过滤实体框架查询的空值

时间:2014-03-13 12:01:38

标签: entity-framework

 var invoice = (from i in Catalog.Invoices
                  where (i.Id == invoiceId)
                  select i)
                  .Include(i => i.Appointment.Service)
                  .Include(i => i.Appointment.Allocations.Select(a => a.Service))
                  .Include(i => i.Appointment.Allocations.Select(e => e.Employee))
                  .FirstOrDefault();

var inv = invoice.Appointment.Allocations.Select(e => e.Employee);

enter image description here

我的问题:如何过滤上述查询中的空值?

1 个答案:

答案 0 :(得分:2)

您应该在查询中添加Where子句,以过滤掉null值。

Select之后:

var inv = invoice.Appointment.Allocations.Select(e => e.Employee).Where(e => e != null);

Select之前:

var inv = invoice.Appointment.Allocations.Where(e => e.Employee != null).Select(e => e.Employee);