473,378 Members | 1,207 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,378 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 3660
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.