Click here to Skip to main content
15,918,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to construct a LINQ query to search a complex (well, not simple anyway) collection (Items) for entries (themselves containing a smaller collection) whose sequence of values are equal and in the same order. Then a "probability" property of the first matching subitem should be updated and the element that matches should be removed.

Here is my first attempt. It is not complete but is indicative of where my thinking has been:

Items[i].Probability += Items.SubItems.Where(item => item.SequenceEqual(Items[i])).Select(result => result.Probability);


Some comments of my own on this:

- I get an error
"Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<double>' to 'double'".</double>
So the select statement isn't return a double and i haven't yet hit on the operator that will do that. Is it wrong to look to use LINQ in this manner?
- The Where operator could return multiple elements. I'm not sure how to handle that.
- I have not yet managed to integrate the removal of matched elements once the compared element (Items[i]) has been updated.

I'll be very grateful to anyone that can offer useful input/guidance on how I should handle this query as I'm quite new to LINQ.


Regards,
Dave
Posted
Updated 18-Dec-10 1:33am
v2

1 solution

"i haven't yet hit on the operator that will do that"

You haven't looked very closely then: Single, First, FirstOrDefault, Last, LastOrDefault.
 
Share this answer
 
Comments
DwR 18-Dec-10 9:51am    
Often there will not be a single item found so they don't apply given the current form of the query. I worded that statement inappropriately as it's really superceded by the next point. I'm not concerned about that error tbh.
[no name] 18-Dec-10 11:19am    
You should be concerned with the error. If you provide accurate information your chances of getting an accurate answer are increased.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900