[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
SolvedPrivate

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

Posted on 2015-01-03
5
Medium Priority
?
44 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
[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
  • 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
 
LVL 1

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
 
LVL 1

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
 
LVL 1

Author Closing Comment

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

650 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