-1

我在过滤 EF 时遇到空引用异常,但我完全一无所知。

在此处输入图像描述

public IEnumerable<TonalityBatchModel> GetTonalityBatch(int briefID)
{
    try
    {
        var brief = NeptuneUnitOfWork.Briefs.FindWhere(b => b.ID == briefID).FirstOrDefault();

        if (brief != null && brief.TonalityCriteria != null)
                    {
                        return brief.TonalityCriteria.TonalityBatches
                                    .Select(b => new TonalityBatchModel()
                                        {
                                            BriefID = b.BriefID,
                                            Status = b.TonalityCriteria.IsActive == true ?"Active":"Ended",
                                            BatchID = b.ID,
                                            CompetitorID = b.BriefCompetitorID,
                                            Competitor = brief.BriefCompetitors.Where(i=>i.ID == b.BriefCompetitorID).Select(c=>c.Organisation.Name).First(),
                                            Size = b.BatchSize,
                                            StartDate = b.StartDate,
                                            EndDate = b.EndDate,
                                            IsPublished = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Published?"Yes":"No",
                                            IsCompleted = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Completed ? "Yes" : "No",
                                            IsAssigned = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Allocated ? "Yes" : "No",
                                            ImportantCount = b.TonalityItems.Count(i=> i.IsImportant),
                                            ArticlesCount = b.TonalityItems.Count,
                                            FavourableCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Favourable),
                                            UnfavourableCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Unfavourable),
                                            NeutralCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Neutral)

                                        }).ToList();
                    }

                    return new List<TonalityBatchModel>();

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw;
            }
        }
4

2 回答 2

1

您需要将查询简化为更简单的查询,然后重新开始构建它,直到NullReferenceException发生这种情况。查看您的代码,这里有一些可能的地方(我正在做一些假设,因为我对您的模型一无所知):

Competitor = brief.BriefCompetitors.Where(i=>i.ID == b.BriefCompetitorID).Select(c=>c.Organisation.Name).First()

BriefCompetitors可以为空。c.Organisation可以为空。

IsPublished = b.Lookup_TonalityBatchStatus.ID == (int)TonalityBatchStatus.Published?"Yes":"No",

(和其他类似的行)b.Lookup_TonalityBatchStatus可能为空。

ImportantCount = b.TonalityItems.Count(i=> i.IsImportant),

(和其他类似的行)b.TonalityItems可能为空。

于 2013-08-16T15:46:59.010 回答
0

我相信这是因为您的计数返回空记录。我可能是错的,但这里生成的 SQL 类似于:

INNER JOIN TonalityItems i on i.Lookup_TonalityScoreTypes == x

x的值在哪里(int)TonalitySourceType.Favourable。因为这个连接没有匹配的结果,所以没有什么可做的count。您可以尝试添加?? 0到查询的末尾:

FavourableCount = b.TonalityItems.Count(i => i.Lookup_TonalityScoreTypes.ID ==(int)TonalitySourceType.Favourable) ?? 0,
于 2013-08-16T15:12:37.487 回答