DefaultIfEmpty Linq到Sql,在Moles测试中返回null



我为测试我的存储库类制作了一个moles,当我放入DefaultIfEmpty(new Drivers())时它可以工作,但当我运行程序时,我会得到这个错误:不支持的重载用于查询运算符"DefaultIfEmpty">

但是,当我把它放回DefaultIfEmpty()时,它工作得很好,但我的*strongtext*moles测试现在返回一个null值。。这是我的代码:

var result = from p in this.context.AirPositions
join a in this.context.Airplane p.airplane_id equals a.id
join s in this.context.Status on p.status_id equals s.id
join dp in this.context.DriversPositions on p.id equals dp.position_id into dpJoin
from ds in dpJoin.DefaultIfEmpty(new DriversPosition())
join d in this.context.Drivers on ds.driver_id equals d.id into dsJoin
from drs in dsJoin.DefaultIfEmpty(new Driver())
orderby p.timesent descending
select new PositionViewModel()
{ ... };

linq提供程序似乎不支持DefaultOrEmpty函数的重载,该函数采用默认值。

如果你想用摩尔测试这一点,你必须将你的代码重写为其他东西。我知道该测试不应该更改您的代码。尝试使用SingleOrDefault重写代码。

使用Linq到Sql和Linq到Object运行的代码似乎有问题。

我用以下方法解决了这个问题:

var result = from p in this.context.AirPositions
join a in this.context.Airplane on p.asset_id equals a.id
let driver = (from dd in this.context.DriversPositions.Where(u => u.position_id == p.id)
join d in this.context.Drivers on dd.driver_id equals d.id
select d).FirstOrDefault()
orderby p.timesent descending
select new PositionViewModel() {...}

希望这能帮助其他人:)

最新更新