Solved

How to create nested LINQ query

Posted on 2009-05-12
2
1,030 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jquery tab header text 1 28
transition to visual .net from vb6 5 59
Visual Studio hangs on running project 6 57
Where does legacy ASP.NET initialize language? 2 56
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 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