簡體   English   中英

LINQ:System.Int32是一個不可為空的值類型

[英]LINQ: System.Int32 is a non-nullable value type

錯誤:無法將null值分配給類型為System.Int32且具有非可空值類型的成員。 程序在這里崩潰:

Nullable<Int32> maxTagFrequency = (from t in tagSummary select t.tagCount).Max();

這很奇怪,因為我聲明變量可以為空,int? maxTagFrequency也不起作用......

整個LINQ查詢:

private void BindTagCloud()
{

 int pro_id = Convert.ToInt32(proj_id);

    var tagSummary = from af in db.AgileFactors
               join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
               join s in db.Stories on psf.StoryID equals s.StoryID 
               join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
               join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
               join pro in db.Projects on it.ProjectID equals pro.ProjectID
               where pro.ProjectID == pro_id &&
                     pro.ProjectID == it.ProjectID &&
                     it.ProjectIterationID == pim.ProjectIterationID &&
                     pim.ProjectIterationMemberID == s.ProjectIterationMemberID &&
                     s.StoryID == psf.StoryID &&
                     psf.AgileFactorID == af.AgileFactorID
                     group af by af.Name into tagGroup

                     select new
                     {

                        Tag = tagGroup.Key,
                        tagCount = tagGroup.Count()

                     };

    Nullable<Int32> maxTagFrequency = (from t in tagSummary select t.tagCount).Max();

var tagCloud = from af in db.AgileFactors
                   join psf in db.ProjectStoryFactors on af.AgileFactorID equals psf.AgileFactorID
                   join s in db.Stories on psf.StoryID equals s.StoryID
                   join pim in db.ProjectIterationMembers on s.ProjectIterationMemberID equals pim.ProjectIterationMemberID
                   join it in db.Iterations on pim.ProjectIterationID equals it.ProjectIterationID
                   join pro in db.Projects on it.ProjectID equals pro.ProjectID
                   where pro.ProjectID == pro_id &&
                         pro.ProjectID == it.ProjectID &&
                         it.ProjectIterationID == pim.ProjectIterationID &&
                         pim.ProjectIterationMemberID == s.ProjectIterationMemberID &&
                         s.StoryID == psf.StoryID &&
                         psf.AgileFactorID == af.AgileFactorID
                   group af by af.Name into tagGroup
                   select new
                   {

                       Tag = tagGroup.Key,
                       weight = (double)tagGroup.Count() / maxTagFrequency * 100
                   };

  ListView1.DataSource = tagCloud; 
  ListView1.DataBind();

}

試試這個:

int? maxTagFrequency = (from t in tagSummary select (int?)t.tagCount).Max();

當你將強制轉換放在linq查詢中時,它允許整個結果在必要時為null。

我搜索了“空序列上的linq max”並且以下鏈接是相關的: Max或Default?

特別是在該頁面上是本文的鏈接 - 這提供了有關其工作原理的更詳細說明: http//www.interact-sw.co.uk/iangblog/2007/09/10/linq-aggregates

從使用SQL的X.*功能的存儲過程中提取時,LINQ to SQL中可能會發生此錯誤。

顯式聲明SELECT子句中的每個字段都可以解決此問題。

代替

SELECT * from X

這將在某些情況下修復錯誤

SELECT X.Field1, X.Field2 from X

特別是,當在可空整數字段上定義了外鍵關系時,有時會出現此錯誤。

這也可能是由於通過設計人員在DBML上創建的無效關聯引起的,或者是由此引起的。 在我的情況下,當我試圖創建一對一的表時,我發現了一個錯誤,我把錯誤的表作為父表。 所以發生的事情是我的Linq DBML類期望來自DBML中定義的“父”的記錄。 由於該“父”表實際上不是父表,因此它通常沒有在鏈接兩者的id列上關聯的相應記錄。 所以當發生這種情況時,我的Linq類的填充會在構造/填充結果集中的第3行時出錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM