Solved

How to create nested LINQ query

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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