Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 测试期间Linq出错,但运行时不出错_C#_Linq - Fatal编程技术网

C# 测试期间Linq出错,但运行时不出错

C# 测试期间Linq出错,但运行时不出错,c#,linq,C#,Linq,在我的MVC控制器ActionResult中,我尝试执行一种合并类型的查询。当我运行我的代码时,这是可行的,但是ActionResult的单元测试失败了 首先,我得到一个特定资源的集合,如果有的话。可能没有 然后我得到一个资源列表(projResources),使用DefaultIfEmpty连接到revResources 在测试期间,我在内存中为'System.Linq.IQueryable'类型的projResources获取一个对象,但当我尝试获取列表()时,我得到: 用户代码未处理Nul

在我的MVC控制器ActionResult中,我尝试执行一种合并类型的查询。当我运行我的代码时,这是可行的,但是ActionResult的单元测试失败了

首先,我得到一个特定资源的集合,如果有的话。可能没有

然后我得到一个资源列表(
projResources
),使用DefaultIfEmpty连接到
revResources

在测试期间,我在内存中为'System.Linq.IQueryable'类型的
projResources
获取一个对象,但当我尝试获取列表()时,我得到:

用户代码未处理NullReferenceException

我不确定什么是null,或者为什么它在运行时工作,但在测试期间不工作

在测试期间,
db.roleasignment
db.ProfileSnapshot
都具有正确关联的有效记录。我通过尝试
选择ra
选择p
来测试这一点

下面是有问题的代码:

var revResources = (from ra in db.RoleAssignment
                    where ra.RoleId == (int)SystemRoles.viewReview
                    select ra)
                .Distinct();
// At Runtime this may have zero (0) records and still functions.

var projResources = (from ra in db.RoleAssignment
                        join p in db.ProfileSnapshot on ra.AssigneeId equals p.ProfileSnapshotId
                        join r in revResources on ra.AssigneeId equals r.AssigneeId into rres
                        from r in rres.DefaultIfEmpty()
                        where ra.AssignedToId == review.RequestReview.ProjectSubmissionToReview.ProjectId
                        select new ProfileSnapshotViewModel
                        {
                            ProfileSnapshotId = p.ProfileSnapshotId,
                            Salutation = p.Salutation,
                            FirstName = p.FirstName,
                            MiddleName = p.MiddleName,
                            LastName = p.LastName,
                            Email = p.Email,
                            OriginalSelected = r.RoleAssignmentId != null,
                            Selected = r.RoleAssignmentId != null,
                            RoleAssignmentId = r.RoleAssignmentId
                        }).Distinct();

var assignedResList = projResources.ToList(); // This code fails
错误详细信息中的StackTrace如下所示:

at lambda_method(Closure , <>f__AnonymousType17`2 )
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<DistinctIterator>d__81`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at PublicationSystem.Controllers.ProjectController.ReviewResourceDetails(Guid id) in c:\Projects\BitLocker\publicationsystem\PublicationSystem\Controllers\ProjectController.cs:line 1200
at PublicationSystem_Test.Controllers.ProjectTests.Project_ReviewResourceDetails_Test() in c:\Projects\BitLocker\publicationsystem\PublicationSystem_Test\Controllers\ProjectTests.cs:line 74
lambda_方法(闭包,f__匿名类型17`2)下的

在System.Linq.Enumerable.WhereSelectEnumerableInterator`2.MoveNext()中
在System.Linq.Enumerable.d_u81`1.MoveNext()中
位于System.Collections.Generic.List`1..ctor(IEnumerable`1集合)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
在c:\Projects\BitLocker\PublicationSystem\PublicationSystem\Controllers\ProjectController.cs中的PublicationSystem.Controllers.ProjectController.ReviewResourceDetails(Guid id)中:第1200行
在c:\Projects\BitLocker\PublicationSystem\PublicationSystem\u Test\Controller\ProjectTests.Project\ReviewResourceDetails\u Test()中的PublicationSystem\u Test.Controller.ProjectTests.Project\ReviewResourceDetails\u Test()中:第74行
问题在于:

.....
from r in rres.DefaultIfEmpty()
..... 
您已经容忍了
DefaultIfEmpty()
值,这意味着对于
r
条目,LINQ查询可能返回NULL

否则,您试图访问select子句中的
r
属性

 // What should be the value of RoleAssignmentId  if r is null ? 
OriginalSelected = r != null && r.RoleAssignmentId != null,
Selected = r != null && r.RoleAssignmentId != null,
RoleAssignmentId = r == null ? null : r.RoleAssignmentId 
ToList()引发异常,因为LINQ查询仅在readen时运行


关于

r.roleasignmentId可能为空,这是一个非常明确的主题,我同意。在某些情况下,这是正确的。有没有更好的方法允许这样做?为什么它在运行时工作,但在测试期间工作?(我猜是Linq到对象还是Linq到实体?)。