Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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?
  • Posted

    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

    C#
    var q = PaymentsSummary.First(i => i.Amount > 0);


    or something

    Hope this helps
     
    Share this answer
     
    Comments
    Laurence1234 30-Jun-11 9:48am    
    Good solution, thanks.

    It is what I actually used to get round this problem in the end, however I do order the data by the customers number each time, so even if the Database gave the list in a different order, it's processing the same list every time.
    Wayne Gaylard 30-Jun-11 9:51am    
    Maybe within each customer the list of paymentsummaries is not ordered. Anyway glad you got it sorted. Cheers.
    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.
     
    Share this answer
     
    Comments
    Laurence1234 30-Jun-11 11:18am    
    Interesting. Thanks for your input.
    As I do specify the ordering after the data is retrieved though, surely my question regarding the First function is still valid.

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



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