C# 转换为值类型“int32”失败,因为具体化的值为null
以下是我从GameByGameTypes和Categories中获取值的代码,但在名为GameByGameTypes的表中,CategoryId列的值为NULL。所以我想要0代替NULLC# 转换为值类型“int32”失败,因为具体化的值为null,c#,wpf,asp.net-mvc,linq,C#,Wpf,Asp.net Mvc,Linq,以下是我从GameByGameTypes和Categories中获取值的代码,但在名为GameByGameTypes的表中,CategoryId列的值为NULL。所以我想要0代替NULL Category objCategory; var query = (from gametypebygametype in db.GameByGameTypes.Where( x => x.GameTypeId == gametypeid)
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);
}
您有三种方法来处理此问题: 您可以将数据库中的列设置为非空,并将默认值设置为列0 您可以设置类别属性int?分类 您可以将查询更改为使用默认值的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 ...
您有三种方法来处理此问题: 您可以将数据库中的列设置为非空,并将默认值设置为列0 您可以设置类别属性int?分类 您可以将查询更改为使用默认值的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 ...
或者你可以替换
select new
{
category.CategoryId,
category.CategoryName
}
与
如果你使用ReSharper,它会“抱怨”说??不需要0部分。别理它
您使用的Linq代码被转换为SQL。当您执行它时,结果为null,但强类型编译器不能相信这一点,并等待不可为null的值。这是一个失败的“物化”过程。
所以您只需“提示”编译器,它可以为null
.或者您可以替换
select new
{
category.CategoryId,
category.CategoryName
}
与
如果你使用ReSharper,它会“抱怨”说??不需要0部分。别理它
您使用的Linq代码被转换为SQL。当您执行它时,结果为null,但强类型编译器不能相信这一点,并等待不可为null的值。这是一个失败的“物化”过程。
所以您只需“提示”编译器,它可以为null
.尝试使用GetValuerDefault
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);
}
尝试使用GetValuerDefault
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);
}
感谢关于ReSharper的提示,它在尝试解决此问题时将我甩了出去。欢迎ReSharper提示应以警告处理:这只是提示。感谢关于ReSharper的提示,它在尝试解决此问题时将我甩了出去。欢迎ReSharper提示应以警告处理:这只是提示。