locked
Linq Returning Incorrect Result RRS feed

  • Question

  • So maybe I'm doing this wrong, but I have a linq query that looks like this:

    var sTAData = from staItem in dSXE.STAUploads
                   where (staItem.TransactionDate >= StartDate) && (staItem.TransactionDate <= EndDate)
                   orderby staItem.TransactionDate
                   select staItem;
    
    

    Then I have a method that scrubs the data and makes some changes based on certain criteria.

            ScrubSTA(sTAData);
    
    

    One of the things that changes is that the TransactionDate can get moved. However, I want the end result to still have the same date range as the first query, so I do something like this:

            var sTADataToo = from staItem in sTAData
                 where (staItem.TransactionDate >= StartDate) && (staItem.TransactionDate <= EndDate)
                 orderby staItem.TransactionDate
                 select staItem;
    

    The problem is that sTADataToo isn't receiving the filtered data. There are obviously records where the transaction date is outside the range specified in the query.

    Please let me know if I'm doing something wrong.

    Thanks

    Thursday, December 30, 2010 10:50 PM

Answers

  • Hi kettch,

    Welcome to EF forum!

    Generally, the ORDERBY query should be the last LINQ operation on the LINQ to Entities query because other operations may effect the final ORDERBY results.   From your first LINQ query, I think it's a IQueryable<> query which is not really executed until we try to access or enumerate the query result. 

    I would recommend you make the first query as IEnumerable<> one by using .ToList() or ToArray().   Then all the later operations on the query become LINQ to Objects operations.  

    Some references:
    http://msdn.microsoft.com/en-us/library/cc716784.aspx
    http://ramanisandeep.wordpress.com/2009/08/26/iqueryable-vs-ienumerable/

    Please feel free to let me know if you have any questions. 

    Happy New Year!

     


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 31, 2010 2:06 AM
  • Hi Kettch,

         Try this

     

     

    var sTAData = (from staItem in dSXE.STAUploads

     

     

    where (staItem.TransactionDate >= StartDate) && (staItem.TransactionDate <= EndDate)

     

     

    select staItem).OrderBy(c => c.TransactionDate).ThenBy(c => c.TransactionDate).ToList();

     


    Thanks, Mukesh | [Remember to click mark as answered when you get a correct reply to your question]
    Tuesday, January 4, 2011 5:14 AM

All replies

  • Hi kettch,

    Welcome to EF forum!

    Generally, the ORDERBY query should be the last LINQ operation on the LINQ to Entities query because other operations may effect the final ORDERBY results.   From your first LINQ query, I think it's a IQueryable<> query which is not really executed until we try to access or enumerate the query result. 

    I would recommend you make the first query as IEnumerable<> one by using .ToList() or ToArray().   Then all the later operations on the query become LINQ to Objects operations.  

    Some references:
    http://msdn.microsoft.com/en-us/library/cc716784.aspx
    http://ramanisandeep.wordpress.com/2009/08/26/iqueryable-vs-ienumerable/

    Please feel free to let me know if you have any questions. 

    Happy New Year!

     


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 31, 2010 2:06 AM
  • Hi kettch,

    Could you please tell us how is the problem now?  If you need any further assistance, please feel free to let me know.

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 4, 2011 1:41 AM
  • Hi Kettch,

         Try this

     

     

    var sTAData = (from staItem in dSXE.STAUploads

     

     

    where (staItem.TransactionDate >= StartDate) && (staItem.TransactionDate <= EndDate)

     

     

    select staItem).OrderBy(c => c.TransactionDate).ThenBy(c => c.TransactionDate).ToList();

     


    Thanks, Mukesh | [Remember to click mark as answered when you get a correct reply to your question]
    Tuesday, January 4, 2011 5:14 AM