Solved

How to create nested LINQ query

Posted on 2009-05-12
2
1,027 Views
Last Modified: 2013-11-11
I want convert an SQL to a corresponding LINQ query. Please find the SQL query in code section.
Select priority from Active_SLA where APP_ID =

(Select APP_ID from FORM_PAGES where PAGE_ADDRESS = @address) and

PERSON_ID = (Select PERSON_ID from PERSON_DEVICES where DEVICE_NUMBER = @number)

Open in new window

0
Comment
Question by:Amitabh_Basu
2 Comments
 
LVL 15

Expert Comment

by:Walter Ritzel
ID: 24365061
If you really want the query ready for use, you'll need to provide more info, like the entity model for us to understand how do you map those tables.

If you look for a generic answer, I have this one:
Suppose two entities: Blog_Post and Blog_Post_Tags, where one Post could have many Tags associated with it.

To select posts based on tags, I would wirte a SQL statement like this:

SELECT * from blog_posts
where post_id in (select post_id from blog_post_tags where tag_name = @tag)

In LINQ, this would be like that:

var posts = from p in Context.Posts
                   where p.Tags.Contains(_tag)
                   select p;

Hope this helps!
0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 500 total points
ID: 24370341
Rather than nested queries, you should use join statements to combine tables based on matching columns.

In your example, the proper Linq query would look something like this:
from a in Active_SLA
        join f in FORM_PAGES on a.APP_ID equals f.APP_ID
        join p in PERSON_DEVICES on a.PERSON_ID equals p.PERSON_ID
        where (f.PAGE_ADDRESS == @Address) && (p.DEVICE_NUMBER == @number)
select a.PRIORITY;

Hope that helps!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

910 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