471,337 Members | 1,242 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

LINQ to SQL - Help with restructuring a query

Hi -

I'm trying to get my head around LINQ to SQL and the new syntax. I have a
query that's working, but I have a feeling that it's not as clean as it could
be. I would like some guidance on how to improve it.

In short, there's a table that can have multiple entries for a single
respondent and I want the record that they entered last (i.e.
MAX(ResponseDate))

In T-SQL, it would look like this:

SELECT [xml] FROM tblRawResults r
WHERE r.RespondentId = 111111
AND r.ResponseDate =
(
SELECT MAX(ResponseDate) FROM tblRawResults r
WHERE r.RespondentId = 111111
)

I have translanted the query to LINQ as follows:

output = db.RawResults.Single
(
r1 =r1.RespondentId == p_RespondentId &&
r1.ResponseDate ==
(
(from r in db.RawResults
where r.RespondentId == p_RespondentId
select r.ResponseDate).Max()
)
).Xml;
I have suspect that there's a way to do this w/o the use of that subquery
that T-SQL requires. Somehow integrating that MAX into the main query. Am I
correct, or is this the proper way to translate it?

Thanks for your input.
--
-Paul Prewett
Apr 8 '08 #1
1 1148
Have you tried hooking the data-context's Log into Console.Out? What
does the TSQL look like?

Marc
Apr 8 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Marshal | last post: by
28 posts views Thread by Marc Gravell | last post: by
4 posts views Thread by BeSharp | last post: by
8 posts views Thread by Andy | last post: by
reply views Thread by =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post: by
14 posts views Thread by thj | last post: by
9 posts views Thread by Cirene | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.