Solved

Dealing with Null values in a database when using linq.

Posted on 2014-02-18
6
626 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c# regex: extract & replace text between braces 6 45
How to deploy to IIS 7 on 2008R2 server with DUO 2-step login? 2 37
c# LinkButton OnClientClick 2 34
Server Error 11 48
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now