471,344 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

LINQ to object query construction

After having played around with LINQ and reading the literature on it I have
not been successful in getting this to work so I am looking for some guidance:

I want to find a hunk in a collection whose Start field is the largest in
the collection but less than an arbitrary cutoff ("selectedRow"). The result
of the query should just be that single object of type Hunk. (No two objects
will have the same value for their Start fields.) Here is the code that I
thought might work:

Hunk myHunk =
(Hunk) from h in hunkList
where h.Start < selectedRow
group h by h.Start into g
select g.Max(g2 =g2.Start);

This seems to have two issues:
(1) Runtime error on the cast to Hunk so apparently I am not selecting a
single Hunk.
(2) If I store the result just in a var, the debugger shows me that there
are no objects in the result collection.

To be clear on what I am seeking, here is a SQL-equivalent version of what I
want:

select * from hunkList
where start =
(select max(start) from hunkList
where start < selectedRow)
)

Thanks!
Jun 27 '08 #1
5 3565
michael sorens <m_********@newsgroup.nospamwrote:
After having played around with LINQ and reading the literature on it I have
not been successful in getting this to work so I am looking for some guidance:

I want to find a hunk in a collection whose Start field is the largest in
the collection but less than an arbitrary cutoff ("selectedRow"). The result
of the query should just be that single object of type Hunk. (No two objects
will have the same value for their Start fields.) Here is the code that I
thought might work:

Hunk myHunk =
(Hunk) from h in hunkList
where h.Start < selectedRow
group h by h.Start into g
select g.Max(g2 =g2.Start);

This seems to have two issues:
(1) Runtime error on the cast to Hunk so apparently I am not selecting a
single Hunk.
You're selecting the maximum value of Start, not the Hunk with the
maximum size. But the result would be an IEnumerable<int(assuming
Start is of type int - you get the idea) rather than a single int.

(The result of the group ... into g is an
IEnumerable<IGrouping<int,Hunk>and you're then calling Max on each of
those IGrouping<int,Hunk>.)
(2) If I store the result just in a var, the debugger shows me that there
are no objects in the result collection.

To be clear on what I am seeking, here is a SQL-equivalent version of what I
want:

select * from hunkList
where start =
(select max(start) from hunkList
where start < selectedRow)
)
I would split it into two parts:

int maxStart = hunkList.Where(hunk =hunk.Start < selectedRow)
.Max(hunk =hunk.Start);

Hunk maxHunk = hunkList.Where(hunk =hunk.Start == maxHunk)
.Single();

(There are alternatives where you could write your own Max method which
returns the element with the maximum projected value, etc.)

Of course, I could have mistaken everything completely - I'm somewhat
tired at the moment. It would help to have an example - could you
produce a short but complete program demonstrating the problem, and
with a small amount of sample data?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
Thanks to both Jon and Linda--you both provided useful ideas for me.

Follow-up question: could you recommend a good link for learning more about
LINQ? Web searches have revealed various small tutorials but I have not yet
come across a high-quality, comprehensive one.
Jun 27 '08 #3
Which aspect of LINQ?

The later sections in Jon's book ("C# in Depth", Manning) does a pretty
solid job of introducing the main LINQ operations (including an appendix
of the standard LINQ query operators), and the meaning of the language
syntax (since Jon's is a language book). This *mainly* considers
LINQ-to-objects, but touches on the other LINQ uses.

For database work, "LINQ in Action" (also Manning) is a good read; this
book is dedicated to LINQ, so should give you what you want - but it
isn't as detailed in terms of /how/ LINQ works (at the language level etc).

Marc
Jun 27 '08 #4
Thanks, Marc and Jon, for the book pointers!
Jun 27 '08 #5
Hi Michael,

Thank you for your reply!

For high-quality and comprehensive learning materials for LINQ, see the
following MSDN document:

'Language-Integrated Query (LINQ)'
http://msdn.microsoft.com/en-us/library/bb397926.aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Marc Gravell | last post: by
9 posts views Thread by =?Utf-8?B?RXZlcnQ=?= | last post: by
1 post views Thread by john | last post: by
1 post views Thread by 0to60 | last post: by
8 posts views Thread by Andy | last post: by
15 posts views Thread by shapper | last post: by
5 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
reply views Thread by =?Utf-8?B?SHlwZXJjb2Rlcg==?= | 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.