Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dealing with Null values in a database when using linq.

Posted on 2014-02-18
6
Medium Priority
?
682 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 total points
ID: 39873844
Use the following to retrieve the value from a nullable int:

ModelName = x.CostCenterID.Value
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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