SolvedPrivate

C# LINQ Equivalent of Select * From FruitMix Where Id Not in(Select FruitMixId From Salads)

Posted on 2015-01-03
5
33 Views
Last Modified: 2016-02-15
I have simple SQL database which has two tables, namely, Salads and FruitMix. Salads Table has a Field named FruitMixId which is a Foreign Key from Fruit Mix table FruitMix table. I have SQL query which gives me Id of those records in FruitMix table which are not in the Salads Table. This SQL query is as follows:
Select * From FruitMix Where Id Not in(Select FruitMixId From Salads)

I would like to have a LINQ query that will give me the same result, that is return me the those Ids from FruitMix table which are not FruitMixId field of any record in the Salads table.
I need this LINQ query (ideally) in C#.

Thank you for your help in anticipation.
0
Comment
Question by:FaheemAhmadGul
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:Camillia
ID: 40529550
I think you can use one of these

YourDataContext dc = new YourDataContext(); 
var query =    
    from f in dc.FruitMix   
    where !(from s in dc.Salads   
            select s.ID)    
           .Contains(f.ID)    
    select c;

Open in new window


Or
var results = FruitMix.Except(Salads);

Open in new window

0
 

Author Comment

by:FaheemAhmadGul
ID: 40529581
Thank you for your prompt response to my request for help. Your first suggested code shows an error on the last line (line 7). It says "the name "c" does not exist in the current context.  I could not follow the second suggestion, and it shows errors as well.
0
 
LVL 7

Expert Comment

by:Camillia
ID: 40529607
Change "c" to "f" in  select c;

For second one, you need to add your datacontext

var results = dc.FruitMix.Except(dc.Salads);
0
 

Accepted Solution

by:
FaheemAhmadGul earned 0 total points
ID: 40530756
The above did not work either. I have however, have been able to solve the problem with help from a friend.
The correct solution is as follows:
 var que = context.FruitMixes.Where(c => context.Salads.All(x => x.FruitMixID != c.id)).ToArray();

Open in new window

0
 

Author Closing Comment

by:FaheemAhmadGul
ID: 40539880
The above solution that I got from a friend solves the problem I had.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

803 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