tags:

views:

147

answers:

2
    [Invoke]
    public List<string> GetConCurrentContractId(string identity, string empId, string payMonth)
    {
        List<string> _rtn = new List<string>();

        IQueryable<mContract> query = this.ObjectContext.mContract;

        IQueryable<mContract> query2 = this.ObjectContext.mContract.Where(
            q => q.wEmpId == empId && q.wEmpId == "NOTVALID");
        if (query.Count()>0)
        {
            _rtn.ToList<string>();
        }
        return _rtn;
    }

query has record return, and query.Count() work, and query2.count() return exception ...

What is optional way to know any record return?

+1  A: 

From your title I'm guessing you get a NullReferenceException. The most likely way I can see this happening only for query2 is if one of the items in mContract is null. To ignore these null objects you can do this:

IQueryable<mContract> query2 = this.ObjectContext.mContract.Where(
     q => q != null && q.wEmpId == empId && q.wEmpId == "NOTVALID");
Mark Byers
Well, it could also happen if q was non-null and `wEmpId` is a non-trivial property that throws, but I think you're correct.
Marc Gravell
Good point - if my suggestion doesn't work that would be a good place to look next (though having the stacktrace would help a lot). I've also reworded my comment from 'only way' to 'most likely way'.
Mark Byers
A: 

1) Why: because tha autors implemented the method this way.

2) How to solve it: You can create your own extensions method, which accepts null in IEnumerable argument and returns original instance or empty collection (array) for null value:

public static IEnumerable<T> NotNullEnum<T>( this IEnumerable<T> o ) {
    if ( object.ReferenceEquals( o, null ) {
        return new T[0];
    }
    else {
        return o;
    }
}

IQueryable<mContract> query2 = this.ObjectContext.mContract.NotNullEnum().Where(
        q => q.wEmpId == empId && q.wEmpId == "NOTVALID");
var count = query2.Count();
TcKs