Solved

Dealing with Null values in a database when using linq.

Posted on 2014-02-18
6
642 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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