Solved

Dealing with Null values in a database when using linq.

Posted on 2014-02-18
6
663 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

728 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