LINQ查询-寻求施工帮助 [英] LINQ Query - construction help sought

查看:66
本文介绍了LINQ查询-寻求施工帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试构造一个LINQ查询,以搜索复杂的(或者,无论如何都不简单)集合(项)以查找值序列相同且顺序相同的条目(它们本身包含一个较小的集合).然后,应该更新第一个匹配子项目的概率"属性,并删除匹配的元素.

这是我的第一次尝试.它不完整,但表明我的想法在哪里:

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>

,所以select语句没有返回双精度数,而且我还没有找到执行该操作的运算符.以这种方式使用LINQ是错误的吗?
-Where运算符可以返回多个元素.我不确定如何处理.
-一旦比较的元素(Items [i])已更新,我还没有设法集成对匹配元素的删除.

我将非常感谢任何对LINQ陌生的人都可以提供有用的输入/指南,说明如何处理该查询.


问候,
Dave

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

推荐答案

我还没有找到要执行此操作的运算符"

那时您还没有仔细观察:Single,First,FirstOrDefault,Last,LastOrDefault.
"i haven''t yet hit on the operator that will do that"

You haven''t looked very closely then: Single, First, FirstOrDefault, Last, LastOrDefault.


这篇关于LINQ查询-寻求施工帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆