Wednesday, April 13, 2011

Linq: IsNot in Object collection

Dim goodCustObjList As New List(Of CustomerObj)
goodCustObjList = DataBLLModule.GetCustomerRecordList(String.Empty)
Dim custList = From t In goodCustObjList _ 
               Where t.ID.ToString() IsNot Guid.Empty.ToString() _
               Select t

I have a list of CustomerObj and if the ID (GUID) is not empty then i want to select that object. I did a similar query but the condition is on another object's property (integer) that match if it is 1 or 2 then select it.

Can someone point out what i'm doing wrong on the above linq statement? if you going to test an condition in linq. Isn't IsNot is the correct statement to test that condition?

Jack

From stackoverflow
  • "IsNot" in VB means to test whether two object references point to different objects. I'm not sure why you are getting this for this particular syntax. Can you post the definition of CustomerObj?

    What you really want to be doing though is comparing the .ID property directly.

    Where t.Id <> Guid.Empty
    

    This is the most reliable way of comparing GUID values. Comparing their String values is much slower and can be thrown off if you accidentally do a case sensitive comparison.

    Tom Anderson : Quite right on that comment, this is the proper way.
    Jack : that's it. Thank for the help

0 comments:

Post a Comment