Solved

How to create nested LINQ query

Posted on 2009-05-12
2
1,026 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

16 Experts available now in Live!

Get 1:1 Help Now