SolvedPrivate

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

Posted on 2015-01-03
5
34 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL 10 35
Need help with T-SQL on SQL Server 2014 9 37
SSMS Opening Mode 9 20
SQL Query 2 34
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

827 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