【问题标题】:Cast to value type 'int32' failed because the materialized value is null转换为值类型“int32”失败,因为具体化值为 null
【发布时间】:2013-04-30 08:47:29
【问题描述】:

以下是我从 GameByGameTypes 和 Categories 中获取值时的代码,但在名为 GameByGameTypes 的表中,CategoryId 列的值为 NULL。所以我想要 0 代替 NULL

  Category objCategory;
                var query = (from gametypebygametype in db.GameByGameTypes.Where( x => x.GameTypeId == gametypeid)
                             join category in db.Categories
                             on gametypebygametype.CategoryId equals category.CategoryId into joined
                             from category in joined.DefaultIfEmpty()
                             select new 
                             {
                                 category.CategoryId ,
                                 category.CategoryName
                             }
                             ).Distinct();
                List<Category> objlistCategory = new List<Category>();
                foreach (var item_temp in query)
                {
                    objCategory = new Category();
                    objCategory.CategoryId = item_temp.CategoryId;
                    objCategory.CategoryName = item_temp.CategoryName;
                    objlistCategory.Add(objCategory);

                }

【问题讨论】:

    标签: c# wpf asp.net-mvc linq


    【解决方案1】:

    或者你可以替换

    select new 
    {
        category.CategoryId,
        category.CategoryName
    }
    

    select new 
    {
        CategoryId = category.CategoryId ?? 0,
        CategoryName = category.CategoryName ?? "",
    }
    

    如果您使用 ReSharper - 它会“抱怨”不需要 ?? 0 部分。忽略它。

    您使用的 Linq 代码被翻译成 SQL。当您执行它时 - 结果为空,但强类型编译器不能相信这一点,并等待不可为空的值。 (这是“物化”的过程,失败了)。 所以你只是'提示'编译器,它可能是空的。

    Here is related question.

    【讨论】:

    • 感谢有关 ReSharper 的提示,它在尝试解决此问题时让我失望。
    • Welcome) ReSharper 提示应谨慎对待:它只是提示。
    【解决方案2】:

    你有三种方法来处理这个:

    1. 您可以在数据库not null中设置列,并将默认设置为第0列。

    2. 您可以设置类别属性int? CategoryId

    3. 您可以更改查询以使用具有默认值的 DefaultIfEmpty

              var query = (from gametypebygametype in db.GameByGameTypes.Where( x => x.GameTypeId == gametypeid)
                           join category in db.Categories
                           on gametypebygametype.CategoryId equals category.CategoryId into joined
                           from category in joined.DefaultIfEmpty( new 
                                                                   {
                                                                      CategoryId=0, 
                                                                      Categoryname=""
                                                                   })
                           select new ...
      

    【讨论】:

      【解决方案3】:

      尝试使用 GetValueOrDefault

      Category objCategory;
                              var query = (from gametypebygametype in db.GameByGameTypes.Where( x => x.GameTypeId == gametypeid)
                                           join category in db.Categories
          on new {A = gametypebygametype.categoryID.GetValueOrDefault(0)}
                      equals new {A = category.categoryID}
           into joined
                                           from category in joined.DefaultIfEmpty()
                                           select new 
                                           {
                                               category.CategoryId ,
                                               category.CategoryName
                                           }
                                           ).Distinct();
                              List<Category> objlistCategory = new List<Category>();
                              foreach (var item_temp in query)
                              {
                                  objCategory = new Category();
                                  objCategory.CategoryId = item_temp.CategoryId;
                                  objCategory.CategoryName = item_temp.CategoryName;
                                  objlistCategory.Add(objCategory);
      
                              }
      

      【讨论】:

        猜你喜欢
        • 2013-06-18
        • 1970-01-01
        • 2013-03-18
        • 2013-02-24
        • 2015-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多