Advertisement

12.20.2007 at 01:41AM PST, ID: 23035405
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

LINQ SELECT IN
Tags: select
I'd like to mae an IN query in LINQ. I've found a sample, but I need more.

THIS WORKS:

SQL:
SELECT [t0].[ProductID]
FROM [dbo].[Product] AS [t0]
WHERE ([t0].[ProductID]) IN (@p0, @p1, @p2, @p3)

LINQ:
from p in db.Products
                            where
                                new int[] { 5, 7, 8, 12 }.Contains(p.ProductID)
                            select p


MY QUESTION: HOW TO DO THE FOLLOWING IN LINQ?

select Field1 from Table1 where Table1.Field1 not in (select Fieldx from Tablex) and Table1.Field1 not in (select FieldY from TableY)
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: coyotee
Solution Provided By: Koocurrent
Participating Experts: 2
Solution Grade: A
Views: 213
Translate:
Loading Advertisement...
12.20.2007 at 07:07AM PST, ID: 20507137

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.28.2007 at 02:06AM PST, ID: 20540751

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.28.2007 at 08:11AM PST, ID: 20542762

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.01.2008 at 07:09AM PST, ID: 20559933

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.01.2008 at 07:12AM PST, ID: 20559959

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
12.20.2007 at 07:07AM PST, ID: 20507137
Well, this doesn't generate quite the same query, but it should be equivalent...

from p in data.Products
                            where
                                   (from tablex in data.Tablex select tablex.Fieldx).Contains(p.ProductID.Value)
                            select p).CommandText

This generates the following query:

SELECT [t0].[ProductID]
FROM [dbo].[Product] AS [t0]
WHERE NOT (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[Tablex] AS [t1]
    WHERE [t1].[Fieldx] = ([t0].[ProductID])
    ))

NOT (EXISTS( SELECT...)) should be equivalent to your original query, so I suspect this will do the trick for you. Note that I'm not looking any of this up, I'm just reading your original query and thinking about how I would represent it in C#...LINQ is really extremely intuitive in this way. In C#, you use the Contains() method to find out if one value belongs to a collection of other values -- if you want to find out if it is NOT in that collection, you check !Contains(). If you want to append multiple clauses together, you use "&&" -- the same applies here.
 
12.28.2007 at 02:06AM PST, ID: 20540751
Thank you for theproposed solution, but I see that LINQ does not support WHERE IN or WHERE NOT IT.
 
12.28.2007 at 08:11AM PST, ID: 20542762
How do you figure? I already gave you an example that uses WHERE IN, which you posted in this question -- simply putting a ! in front of the Contains() call in that sample will give you WHERE NOT IN. It's true that it doesn't seem to be possible to generate the exact query you're asking about here, but the EXISTS() or NOT EXISTS() approach is not only equivalent in terms of functionality to, but also more efficient than the WHERE IN (SELECT) approach because the search will be indexed (rather than selecting a list of all the possible values and then searching that unindexed list, it simply searches the original table in the first place). Is there some way in which this query is not sufficient for your needs?
 
01.01.2008 at 07:09AM PST, ID: 20559933
There are a number of ways of doing where in and where not in.

You could use the contains method as stated above or you could use the count idea with sub queries. The idea is if the number of records returned is 0 then it does not exists.

The sample code below gives the linq equivalence of the exact query you are asking for

Try this:
1. Create a project
2. Add a list box and set the DataMember property of id
3. Add a button and double click to add the event handler
4. Add the code snippet below to the form making any necessary changes

PS: The sample linq queries are named as CalcWithFirst and CalcWithSecond for the two approaches given: the first uses the contains method and the second the count idea
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
public void LoadSampleData()
        {
            //create and populate the tables: from the database or using linqtosql
            List<ARecord> table1 = new List<ARecord>(); //no from 1 to  20
            for (int i = 1; i <= 20; i++)
                table1.Add(new ARecord(i, i * 5));
            List<ARecord> table2 = new List<ARecord>(); //even numbers;
            for (int i = 1; i <= 20; i++)
                if (i % 2 == 0)
                    table2.Add(new ARecord(i, i * 2));
            List<ARecord> table3 = new List<ARecord>(); //multiples of 3
            for (int i = 1; i <= 20; i++)
                if (i % 3 == 0)
                    table3.Add(new ARecord(i, i * 3));
 
            //DateTime dt = DateTime.Now;
            //TimeSpan elapsed1, elapsed2;
            //int total=10;
            //for (int i = 0; i < total; i++)
            //{
                listBox1.DataSource = CalcWithFirst(table1, table2, table3).ToList();
            //}
            //elapsed1 = DateTime.Now - dt;
            //dt = DateTime.Now;
            //for (int i = 0; i < total; i++)
            //{
            //    listBox1.DataSource = CalcWithFirst(table1, table2, table3).ToList();
            //}
            //elapsed2 = DateTime.Now - dt;
            //MessageBox.Show(string.Format("{0}\n{1}", elapsed1.TotalSeconds, elapsed2.TotalSeconds));
            
        }
        IEnumerable<ARecord> CalcWithFirst(List<ARecord> table1, List<ARecord> table2, List<ARecord> table3)
        {
            var result = from r in table1
                         let x = (from r2 in table2 select r2.id) //select t2 ids
                         let y = (from r3 in table3 select r3.id) //select t3 ids
                         where !x.Contains(r.id) && !y.Contains(r.id) //not in 2 and 3
                         select r;
            return result;
        }
        IEnumerable<ARecord> CalcWithSecond(List<ARecord> table1, List<ARecord> table2, List<ARecord> table3)
        {
            var result = from r in table1
                         let x = (from r2 in table2 where r2.id == r.id select r2.id).Count()
                         let y = (from r3 in table3 where r3.id == r.id select r3.id).Count() 
                         where x == 0 && y == 0
                         select r;
            return result;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            LoadSampleData();
        }
Open in New Window
Accepted Solution
 
01.01.2008 at 07:12AM PST, ID: 20559959
Forgot to add the ARecord structure. Also in the loadsampledata method I have commented out some code i used to testing which of the two is better. I was getting varying results but the second was mostly done in the shorter time. You could uncomment it get see which works best.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
struct ARecord
        {
            public int id;
            public int value;
 
            public ARecord(int id, int value)
            {
                this.id = id;
                this.value = value;
            }
            public override string ToString()
            {
                return id.ToString();
            }
        }
Open in New Window
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628