473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3662
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
9
by: =?Utf-8?B?RXZlcnQ=?= | last post by:
In my (Windows Forms) project I am using strongly typed datatables. Now I am trying to convert my 'rowfilter/group by' query logic to Linq. Most queries I can easily convert, but some I am having...
1
by: john | last post by:
I'm trying to build a LINQ expression that will use a dynamic construction of a LIKE statement in the WHERE clause, it would look something like this in SQL: WHERE TaskGroup Like "*00*" OR...
1
by: 0to60 | last post by:
Let's say we have your basic Invoices and InvoiceItems table. If we load this in with LINQ: var query = from i in db.Invoices select i; When I then loop through my invoices, if I wanna...
8
by: Andy | last post by:
Hi, I'm trying to add a where clause to my query: List<stringtypes = new List<string>(); types.Add( "A" ); types.Add( "B" ); query = query.Where( c =types.Contains( c.Type ) );
15
by: shapper | last post by:
Hello, I have two Lists: A = {ID, Name} = { (Null, John), (Null, Mary), (Null, Andrew), (Null, Peter) } B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4, Andrew) } I want to...
5
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
linq on objects... want to find if the object exist in its collection if I have a loop searching in the collection foreach (myClass r in collections) { if (r.field01 == type && r.field02...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.