Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create nested LINQ query

Posted on 2009-05-12
2
Medium Priority
?
1,036 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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