none
MyVariableCollection.All() not working RRS feed

  • Question

  • I have no confidence in the Queryable.All<TSource> Generic Method this is the method that returns bool if the expression returns for all the set.

    1st problem
    If the source is empty it raises an error.

    2nd problem
    I cant get it to work.

      var vAccReq = from ardx in DataContext.tblAccessRequestDetails
                              where ardx.AccessRequestId == iAccessRequestId
                              select ardx;


     if (vAccReq.All(p => p.RequestDetailStatusID != (int)RequestDetailStatus.Authorized) && vAccReq.Count() != 0)


    When this runs it never returns true even when the data shows it should.  Is this a bug or is it looking at pre updated data or what

    PGF
    Thursday, December 18, 2008 4:51 PM

All replies

  • It's not possible for anyone to know why your 2nd case won't work, since you have not supplied the data. Howver, I can say that queries run against the database will always run against data that's in the database.  Changes that are not yet submitted will not be seen.  Assuming that's what you meant by pre-updated data.

    For the 1st case, all LINQ aggregating operators throw an exception when the source is empty.  It does this if there is no way to represent a result for that empty source.  Usually if the return type of the operator can hold null, the operator will return null when the source is empty, but if it can't it will throw an exception. Most aggregates have a work-around since you can convert the return type to a nullable type, but All always returns bool so no luck there. 

    I think it probably is a bug in LINQ to SQL, since the specification for the LINQ All operator says it should return true if the source is empty.


    Wayward LINQ Lacky
    Sunday, December 21, 2008 9:30 PM
    Moderator