1

我正在尝试优化查询,因此将其插入 LINQPAD 但我不断收到空引用错误,无法将空值分配给 System.Int32 .. 当我在最后注释掉 FolderID 时,错误没有发生时间更长。为什么它假定 FolderID 是 System.Int32,我怎样才能使它成为 Int32?相反,它可以为空,我可以运行查询?

(from groupBundle in GroupBundles
    join userGroup in UserGroups on groupBundle.GroupID equals userGroup.GroupID
    join bundle in Bundles on groupBundle.BundleID equals bundle.BundleID
    where userGroup.UserID == 75
    orderby bundle.BundleName
    select new
    {
        BundleID = bundle.BundleID,
        BundleName = bundle.BundleName,
        BundleIcon = bundle.BundleIcon,
        UseSpecialPlayer = (bundle.UseSpecialPlayer != null && bundle.UseSpecialPlayer == true) ? true : false,
        height = bundle.PuHeight,
        width = bundle.PuWidth,
        UserID = 75,
        CompanyID = 32,
        IsFavorite = ((from f in Favorites where f.FavoriteTypeID == 1 && f.UserID == 75 && f.ActionID == bundle.BundleID select f).Count() > 0) ? true : false,

        //THIS ONE HERE
        FolderID = (from cf in CategoryFolders 
            join folder in Folders on cf.FolderID equals folder.FolderID
            where folder.CompanyID == 32 &&
            cf.CategoryID == bundle.BundleID
            select cf.FolderID).FirstOrDefault()
}).Distinct()
4

3 回答 3

3

将强制转换为 nullable int 添加到正在分配的表达式:

FolderID = (int?)(from cf in CategoryFolders

FolderID 在数据库中可以为空吗?如果不是,这就解释了这一点。

于 2010-11-15T18:59:02.697 回答
0

我假设 FolderId 在某个数据库中,我猜它被声明为可空类型,甚至可能是可空的 Int 列。

于 2010-11-15T18:59:23.890 回答
0

很抱歉,我不能将此作为评论发布,但仅查询此工作有效吗?

    //THIS ONE HERE
    FolderID = (from cf in CategoryFolders 
        join folder in Folders on cf.FolderID equals folder.FolderID
        where folder.CompanyID == 32 &&
        cf.CategoryID == bundle.BundleID
        select cf.FolderID).FirstOrDefault()
于 2010-11-15T19:00:02.653 回答