Solved

LINQ and NOT IN / Contains Subquery Problem

Posted on 2010-08-26
4
1,203 Views
Last Modified: 2013-11-11
I am trying to return a set of data using LINQ and Entity Framework which will only return items where a related item exists and fulfils a set of criteria.

I have the following code:

 
'Gets a list of customer IDs which have callbacks set
                Dim Callbacks = (From f In MyContext.Callbacks.Include("Customer") Where (f.TypeId > 0) Select f.Customer.Id).ToList

                'Select customers who DO NOT have callbacks set
                Customers = From c In MyContext.Customers _
                    Where (c.StatusId = 2) _
                    And Not Callbacks.Contains(c.Id) _
                    Select c

Open in new window


However, I am getting an error:

LINQ to Entities does not recognize the method 'Boolean Contains(Int32)' method, and this method cannot be translated into a store expression.

How can I do this with LINQ? I have tried using one LINQ query (with a nested subquery instead of the Callbacks.Contains(c.Id) ) and two LINQ queries as shown above
0
Comment
Question by:pipelineconsulting
[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
4 Comments
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33529397
You could sole it like this:

Customers = From c In MyContext.Customers _
                    Where (c.StatusId = 2) _
                    Select c

For each cb in Callbacks
    Customers = From c in Customers Where c.ID <> cb.ID
Next

Then, when the query is executed, the SQL will contain a list of OR operators instead of a single IN operator, but it is the only way I have been able to work around this error.
0
 

Author Comment

by:pipelineconsulting
ID: 33529666
That doesn't quite make sense

For each cb in Callbacks
    Customers = From c in Customers Where c.ID <> cb.ID
Next


Compairing c.ID to cb.ID is not correct as they are two different entities. Each customer has several callbacks but I don't think that comparing their primary keys will give me what I need.

I need only the customers that do not have a callback in place.
0
 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 250 total points
ID: 33529691
Sorry,

Since cb is already a customerID, I meant:

For each cb in Callbacks
    Customers = From c in Customers Where c.ID <> cb
Next
0
 

Author Closing Comment

by:pipelineconsulting
ID: 33529842
Seems to do the trick! There is probably a more elegant solution but for now it is fine.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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