0

一部のデータを取得しようとすると、このエラーが発生します。これが私がクエリしている方法です

   using (_realtyContext = new realtydbEntities())
   {
                foreach (int yr in years)
                {
                    var standard = (from feest in _realtyContext.FeeStandards
                                    where feest.Year == yr && feest.FeeCategory.CategoryName == "PropertyFee"
                                    select feest).SingleOrDefault();

                    var chargeItem = (from chrgItem in _realtyContext.PropertyFees
                                      where chrgItem.FeeStandardID == standard.FeeStandardID
                                      && chrgItem.HousholdId == Householder.HouseholdID
                                      select chrgItem).SingleOrDefault();

                        this._propertyFees.Add(chargeItem);
                        this._standards.Add(standard);
                }
   }

例外の詳細:System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。

エラーは2番目のクエリにあります。(varchargeItem)

このクエリはエラーをスローします:var ChargeItem = .. ..

4

2 に答える 2

3

.SingleOrDefault()は、レコードが見つからない場合にnullを返します。次のステートメントでは、nullにならないかのように「standard」を使用しています。

しかし、それは考えられる多くの原因の1つにすぎません...

于 2012-05-03T13:38:07.083 に答える
1

標準がnullかどうかを確認する必要があります。結果がない場合、SingleOrDefault()はnullを返します

于 2012-05-03T13:39:05.980 に答える