Why is LINQPAD assuming this variable is a System.Int32 and how can I change its mind?
I'm trying to optimize a query so I plugged it into LINQPAD but I keep getting a null reference error, can't assign null value to System.Int32.. when I comment out the FolderID one at the end there, the error no longer occurs. Why does it assume that FolderID is a System.Int32 and how can I make it an Int32? instead so it's nullable and I can run the query?
(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()
Add a cast to nullable int to the expression being assigned:
FolderID = (int?)(from cf in CategoryFolders
is FolderID nullable in the database? If not, that explains this.
I'm assuming that FolderId is in a database somewhere, and I would guess that it's declared as a nullable type, perhaps even a nullable Int column.
I'm sorry I can't post this as a comment, but does just querying this work?
//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()
精彩评论