Solved

Dealing with Null values in a database when using linq.

Posted on 2014-02-18
6
632 Views
Last Modified: 2016-02-10
I have a list of cost centers that I want to grab. My DB Admin allows nulls for one of my fields. I can't seem to get around this. What can I do?? How do I get around this? Right now my error is "Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types."

    public class CostCenter
    {
        public string DepartmentID { get; set; }
        public string ModelName { get; set; }
    }

List<CostCenter> DList = new List<CostCenter>();
                DList = (from x in ctx.Inventory where x.CostCenterID != null
                         select new CostCenter { ModelName = x.CostCenterID + "-" + x.ModelName, DepartmentID = SqlFunctions.StringConvert((double)(x.CostCenterID ?? 1)) }).Distinct().ToList();

Open in new window

0
Comment
Question by:NickMalloy
  • 2
  • 2
  • 2
6 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39868036
I'm assuming the CostCenterID is the column that allows null? I'm not sure why you're having any trouble with it because your "where" clause eliminates any rows where the CostCenterID is null.

What is the actual datatype of CostCenterID in the database? Your casting and conversions in the Select seem entirely unnecessary. You've already eliminated any row where the CostCenterID is null, so why is the null coalesce (i.e. ??) necessary?
0
 

Author Comment

by:NickMalloy
ID: 39868722
I started going down that route because of the traditional can't convert ?int to int.
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 39868851
I'm afraid that doesn't give me enough information to provide any further assistance. If you still need help please see the rest of my questions from the first post and provide answers. Thanks.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 3

Expert Comment

by:DaveKeyes
ID: 39869993
If the CostCenterID is an numeric datatype, then you need to use:

where x.CostCenterID.HasValue
0
 

Author Comment

by:NickMalloy
ID: 39870427
I'm trying to get ride of the error.

cannot implicitly convert type 'int? ' to 'int'

I've tried several ways, but don't know how to get around it. CostCenterID allows nulls.

Current Code

List<CostCenter> DList = new List<CostCenter>();
DList = (from x in ctx.Inventory where x.CostCenterID.HasValue
                         select new CostCenter { ModelName = x.CostCenterID + "-" + x.ModelName, DepartmentID = x.CostCenterID }).Distinct().ToList();

Open in new window

0
 
LVL 3

Accepted Solution

by:
DaveKeyes earned 500 total points
ID: 39873844
Use the following to retrieve the value from a nullable int:

ModelName = x.CostCenterID.Value
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question