2

以下是我从 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);

                }
4

3 回答 3

4

Or you can replace

select new 
{
    category.CategoryId,
    category.CategoryName
}

with

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

If you use ReSharper - it will 'complain' that ?? 0 part is not needed. Ignore it.

And Linq-code you use is translated to SQL. When you execute it - result is null, but strongly-typed compiler cannot believe in that, and awaits non-nullable value. (this is the process of 'materialization', that fails). So you just 'hint' compiler, that it could be null.

Here is related question.

于 2013-04-29T22:46:42.710 回答
0

尝试使用 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-04-29T22:56:14.063 回答
0

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

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

  2. 您可以设置 Category 属性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 ...
    
于 2013-04-15T05:10:36.930 回答