SolvedPrivate

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now