有关List类中的First()方法的问题 [英] A question about the First() Method in the List Class
本文介绍了有关List类中的First()方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个快速的问题.
情况如下:
string
属性和int
,这是数据库中该条目的唯一ID.还有一个金额"属性存储为decimal
IEnumerable<IGrouping<TKey, TElement>>
将与客户相关的每个摘要分组
First<T>()
方法填充GroupedPaymentSummary中的相关字段.问题发生在这里:
在我看来,这意味着First函数几乎是随机的.该错误是间歇性的,并且似乎有四分之一的机会选择空白付款作为第一个值.但是,有时其他情况显然是选择了正确输入的付款,并且正确填充了所有值.
我的问题是这样:
我观察到的是真的吗?每次给定相同顺序的相同数据集,为什么First函数选择一个不同的值?
Hi Everyone,
I have a quick question.
The scenario is the following:
string
properties and an int
which is the unique ID for that entry in the database. There is also an "Amount" property stored as a decimal
IEnumerable<IGrouping<TKey, TElement>>
First<T>()
method to populate the relevant field in my GroupedPaymentSummary.The problem occurs here:
This implies to me that the First function is almost random. This error is intermittent and appears that there is a on in 4 chance of choosing the blank payment as the first value. However other times clearly a correctly entered payment is chosen and all the values are populated correctly.
My question is this:
Is what I''m observing true? Given the same data set in the same order every time, why does the First function choose a different value?
推荐答案
如果不指定谓词对于First方法,则它将返回该序列中的第一个元素.可能是您的数据库以随机顺序而不是LinQ方法返回结果.也许尝试使用谓词过滤掉null值,就像这样
If you do not specify a predicate for the First method then it returns the first element in that sequence. It is probably your database that is returning the results in a random order, not the LinQ method. Maybe try using a predicate to filter out the null values ,something like this
var q = PaymentsSummary.First(i => i.Amount > 0);
或什么
希望对您有帮助
or something
Hope this helps
您必须查看数据库中的文档,但是对于大多数数据库,除非明确定义顺序,否则未定义返回行/数据的顺序/sort进行查询.
是的,对于具有非常简单实现的数据库,它每次都会以相同的顺序为您提供相同的结果-但这不是必需的.恰好每次都在做完全相同的事情.
对于更复杂的数据库程序,您可能会按照找到的顺序将数据取回,这可能会受到许多因素的影响.如果数据分布在多台计算机上,或者有多个内核在检索它,那么它只是随机计时.也许某些数据被缓存,而有些则没有.如果数据全部集中在一个地方,并且有一个线程在提取数据,那么一个真正智能的数据库可能会通过提取与当前磁盘头位置最近的记录来最大程度地减少磁盘上的查找时间.
You''d have to look at the documentation on your database, but for most databases, the order of the rows/data returned is not defined unless you explicitly define an order/sort in you query.
For a database with a very simple implementation, yeah, it will give you the same results in the same order each time -- but it''s not required to. It just happens to be doing the exact same thing each time.
For a more complex database program, you might be getting data back in the order it was found, which can be influenced by a number of factors. If the data is distributed on multiple machines, or there are multiple cores retrieving it then it''s just random timing. Or maybe some of the data is cached and some isn''t. If the data is all in one place and there is one thread fetching it, a really smart database might be minimizing seek times on the disk by fetching the record that is nearest the current disk head position.
这篇关于有关List类中的First()方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文