Click here to Skip to main content
15,036,363 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using following query to get data from data base but it is throwing exception saying "An exception of type System.InvalidOperationException occurred in EntityFramework.dll but was not handled in user code
Additional information: The cast to value type System.Int64& failed because the materialized value is null. Either the result types generic parameter or the query must use a nullable type."
********************************************************
C#
var articles = (from art in db.Article_Template
                                join uad in db.Article_Template_Detail on art.USER_ARTICLEID equals uad.USER_TemplateID into aADetail
                                from aad in aADetail.DefaultIfEmpty() orderby art.CREATED_ON descending
                                select new
                                {
                                    art.CONTENTID,
                                    art.ARTICLE_TITLE,
                                    art.ANSWERED,
                                    art.ARTICLE_DESCRIPTION,
                                    art.CONTENT_TYPE,
                                    art.CREATED_BY,
                                    art.CREATED_ON,
                                    art.IP_ADDRESS,
                                    art.ISACTIVE,
                                    art.MODIFIED_BY,
                                    art.MODIFIED_ON,
                                    art.ReleventScore,
                                    art.TAG,
                                    art.TEMPLATEID,
                                    art.SectionID,
                                    art.USER_ARTICLEID,
                                    aad.USERS_Template_DETAILID,
                                    aad.USER_Template_IMAGE_PATH,
                                    aad.USER_Template_IMAGE_THUMNAIL

                                }).Take(1000).ToList();


********************************************************
However when I execute this query the following way(i.e without DefaultIfEmpty()) it works
C#
var articles = (from art in db.Article_Template
                                    join uad in db.Article_Template_Detail on art.USER_ARTICLEID equals uad.USER_TemplateID
                                    orderby art.CREATED_ON descending
                                    select new
                                    {
                                        art.CONTENTID,
                                        art.ARTICLE_TITLE,
                                        art.ANSWERED,
                                        art.ARTICLE_DESCRIPTION,
                                        art.CONTENT_TYPE,
                                        art.CREATED_BY,
                                        art.CREATED_ON,
                                        art.IP_ADDRESS,
                                        art.ISACTIVE,
                                        art.MODIFIED_BY,
                                        art.MODIFIED_ON,
                                        art.ReleventScore,
                                        art.TAG,
                                        art.TEMPLATEID,
                                        art.SectionID,
                                        art.USER_ARTICLEID,
                                        uad.USERS_Template_DETAILID,
                                        uad.USER_Template_IMAGE_PATH,
                                        uad.USER_Template_IMAGE_THUMNAIL

                                    }).Take(1000).ToList()



I need to how I can fix this as my client is waiting and I'm stuck in this issue.

Thanks
Sohaib Javed
Posted
Updated 22-Nov-15 1:13am
v5
Comments
Kornfeld Eliyahu Peter 22-Nov-15 7:13am
   
What is the default value you think it returns when empty? (I think it is NULL too)
Sohaib Javed 22-Nov-15 7:19am
   
no if I execute it without DefaultIfEmpty() it works
Sohaib Javed 22-Nov-15 8:42am
   
I'm doing all above for outer join if we have any other way to achieve outerjoin then please let me know. I will appreciate your respone
Ehsan Sajjad 22-Nov-15 10:15am
   
due to outer join you may have null in art where rows have not matched
Sohaib Javed 22-Nov-15 14:42pm
   
Actually art.USER_ARTICLEID is primary key of article table and uad.USER_TemplateID is foreign key in AticleDetail table. and are not null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900