SolvedPrivate

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

Posted on 2015-01-03
5
35 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

733 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