有关List类中的First()方法的问题 [英] A question about the First() Method in the List Class

查看:81
本文介绍了有关List类中的First()方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个快速的问题.

情况如下:

  • 我正在使用LINQ to XRM查询CRM数据库.
  • 我填充了自己类型的列表; 付款摘要".此类型包括一些string属性和int,这是数据库中该条目的唯一ID.还有一个金额"属性存储为decimal
  • 为了使这些数据更易于阅读,我使用IEnumerable<IGrouping<TKey, TElement>>
    将与客户相关的每个摘要分组
  • 然后填充"GroupedPaymentSummary"列表,这样,如果某个客户在某个日期有5笔2英镑的应付款,它将表明他们总共欠10英镑,以及借记卡所需的详细信息帐户.
  • 由于每个PaymentsSummary在按客户ID分组时都包含同一客户的银行详细信息,因此我使用First<T>()方法填充GroupedPaymentSummary中的相关字段.

    问题发生在这里:

  • 有时候,由于用户错误,数据无法正确输入到此数据库中.
  • 一个客户有4笔付款.其中3个包含借记帐户所需的数据.除客户和价值外,还有1条记录留空.没有银行详细信息.
  • 在尝试报告此数据时,存在空引用异常,因为似乎留空的记录被选为第一个值".
  • 但是而无需处理数据或更改代码并再次运行显示功能,也不例外,并且客户可以显示所有相关信息.

    在我看来,这意味着First函数几乎是随机的.该错误是间歇性的,并且似乎有四分之一的机会选择空白付款作为第一个值.但是,有时其他情况显然是选择了正确输入的付款,并且正确填充了所有值.

    我的问题是这样:

    我观察到的是真的吗?每次给定相同顺序的相同数据集,为什么First函数选择一个不同的值?
  • Hi Everyone,

    I have a quick question.

    The scenario is the following:

  • I''m querying a CRM database using LINQ to XRM.
  • I populate a List of my own type; "PaymentsSummary". This type includes some 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
  • To make this data more human readable I group each summary by the customer they relate to using IEnumerable<IGrouping<TKey, TElement>>
  • I then populate my "GroupedPaymentSummary" List, such that if a customer had 5 payments of 2 pounds due on a certain date, it would show that they owe 10 pounds overall, alongside the details necessary to debit their account.
  • As each PaymentsSummary contains bank details for the same customer when grouped by the customers ID, I use the First<T>() method to populate the relevant field in my GroupedPaymentSummary.

    The problem occurs here:

  • There are times when the data is not correctly entered into this database due to user error.
  • One customer has 4 payments. 3 of which contain the data that is necessary to debit their account. 1 record was left blank apart from the customer and value. No bank details.
  • When trying to report on this data there is a null reference exception as it would seem that the record that was left blank was chosen as the First value.
  • However without manipulating the data or changing the code and running the display function again, there is no exception and that customer has all of the relevant information displayed.

    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屋!

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