Given a class
[Table ( Name = "AllPlayerInfo" ) ]
public class AllPlayerInfo
{
[Column (IsPrimaryKey = true)]
public decimal Classes_ID { get; set; }
[Column (IsPrimaryKey = true)]
public decimal Member_ID { get; set; }
//...
}
I call
DataContext db2 = new DataContext ( sqlconnectstring );
Table<AllPlayerInfo> api = db2.GetTable<AllPlayerInfo> ();
which returns all the records from the database table.
I look at the contents of api and confirm that the record I want is there and has Member_ID == 4617.
So I issue following command:
AllPlayerInfo attempt1 = api.Where ( r => r.Member_ID == 4617).FirstOrDefault<AllPlayerInfo> ();
that (incorrectly) returns null.
So then I transfer the object to a list:
List<AllPlayerInfo> listapi = api.ToList<AllPlayerInfo> ();
and then use the Exists method and it does find the record I wanted:
bool recordexists = listapi.Exists ( r => r.Member_ID == 4617 );
recordexists is true!
So the question is: Why does the Where method not find the record when it is clearly there? I did try the Where method with other fields and values and they worked. Just not this particular field. I tested other decimal fields such as the class_id and it worked.
I would check the generated SQL in SQL profiler. Sometimes when LINQ to SQL determines you are comparing incorrect types, e.g.
Where(x => x.somestring == null)and x.somestring is not set as nullable in the DBML file then it generates SQL with a where clause that saysWHERE 1=0and then goes off and runs that in SQL server which will boviously return no records. Member ID is a decimal and it is probably inerpretting your literal as an int so it has decided thatWhere( r => r.Member_ID == 4617)cannot ever be true so it creates SQL that will never return any records.