[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Concat or Union two or more entities LINQ

Posted on 2010-11-08
2
Medium Priority
?
1,745 Views
Last Modified: 2012-05-10
Hello,

i am trying to do a concat or union between two or more statements, but I am getting this error

Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[VB$AnonymousType_22`2[System.Int32,System.Decimal]]' to type 'System.Collections.Generic.IEnumerable`1[VB$AnonymousType_22`2[System.Int32,System.Nullable`1[System.Decimal]]]'.


In the code below you will see how I have created three seperate linq quiries that provide me with the value based on dt1 and dt2
NOTE: dt1, dt1 are strings that have date values (i.e. start date, end date )


I would like to get the (sum) of  ArAmount between all three tables in one query.


Thank you in advance
Dim qry1 = (From tr In DataContext.TransactionMain _
                              Where tr.TransactionDate >= dt1 _
                              And tr.TransactionDate <= dt2 _
                              And tr.TransactionType = Enums.TransactionType.Completed _
                              Select New With {.arAmount = tr.Amount})


Dim qry2 = (From cm In DataContext.CustomerAccountMemo _
                           Where cm.DateIssued >= dt1 _
                           And cm.DateIssued <= dt2 _
                           Select New With {.arAmount = cm.Amount})

Dim qry3 = (From pm In DataContext.Payment _
                        Where pm.CreatedOn >= dt1 _
                        And pm.CreatedOn <= dt2 _
                        Select New With {.arAmount = pm.Amount * -1})


'Now when I try to merge them as below I get an error Unable to cast object of type 'System.Data.Objects.ObjectQuery`1

Dim qry = (From tr In DataContext.TransactionMain _
                              Where tr.TransactionDate >= dt1 _
                              And tr.TransactionDate <= dt2 _
                              And tr.TransactionType = Enums.TransactionType.Completed _
                              Select New With {.id = tr.TransactionID, .arAmount = tr.Amount}) _
                          .Concat _
                          (From cm In DataContext.CustomerAccountMemo _
                           Where cm.DateIssued >= dt1 _
                           And cm.DateIssued <= dt2 _
                           Select New With {.id = cm.AccountMemoID, .arAmount = cm.Amount}) _
                       .Concat _
                       (From pm In DataContext.Payment _
                        Where pm.CreatedOn >= dt1 _
                        And pm.CreatedOn <= dt2 _
                        Select New With {.id = pm.PaymentID, .arAmount = pm.Amount * -1})


'My ultimate result is that I will have the sum of arAmount between all three tables

Open in new window

0
Comment
Question by:Teknosoft
[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 Comments
 
LVL 7

Accepted Solution

by:
deadlyDev earned 1000 total points
ID: 34091807
There is a mix of types in your query.

At least one of your query (I cannot be sure which without seeing your dbml) fields is a nullable decimal, and the other, or other two are plain decimals.

For whichever fields are not nullable, you should cast the return values to nullable, ie:

Select New With {.id = cm.AccountMemoID, .arAmount = CType(cm.Amount, System.Nullable(Of System.Decimal))}) _
0
 

Author Closing Comment

by:Teknosoft
ID: 34095309
You have identified and directed me to the source of the problem. One entity did have the amount as nullable=false. by just changing that I was able to use the linq query.

Thanks for your help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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