是否可以更改此查询以使其不返回Ienumerable List但只返回单个对象? 在我的情况下,它保证单个对象或null。
.First()
似乎无法使用?
var as= from s in entities.Subscriptions
join or in entities.OrderRates on s.OrderRateId equals or.OrderRateId
join oo in entities.OrderOfferings on or.OrderOfferingId equals oo.OrderOfferingId
join ov in entities.OrderVendors on oo.OrderVendorId equals ov.OrderVendorId
where s.CustomerId == customerId
select new
{...}
答案 0 :(得分:0)
SingleOrDefault
将抛出异常。因此,如果您的where条件没有产生任何项目,您将获得例外。
您可以使用 var customer = (from s in entities.Subscriptions
join or in entities.OrderRates on s.OrderRateId equals or.OrderRateId
join oo in entities.OrderOfferings on or.OrderOfferingId equals oo.OrderOfferingId
join ov in entities.OrderVendors on oo.OrderVendorId equals ov.OrderVendorId
where s.CustomerId == customerId
select new
{ //your mapping for new annonymous object goes here}
).SingleOrDefault();
方法,如果您确定只有一个项目或从您的where条件产生空值。
SingleOrDefault
FirstOrDefault
/ System.Linq
位于using System.Linq;
命名空间中。因此,请确保使用using语句将该命名空间导入到使用此
preg_replace