I have simple customer table that has 4 records, and only one has email
public string Username { get; set; }
public string Email { get; set; }
............
var data = cn.Query<Customer>("select * from customer");
var aaa= data.FirstOrDefault(f => f.Email.ToLower() == "admin@yourstore.com"); >> ok
var bbb= data.FirstOrDefault(f => f.Email.ToLower() == "kjhkh");
since other items have email = null
, so it gets:
Object reference not set to an instance of an object
Why it's that tricky to use linq with Dapper, am i doing wrong ?
You could use string.Equals()
with a StringComparison
type:
var a = data.FirstOrDefault(f => string.Equals(f.Email, "test@test.com", StringComparison.OrdinalIgnoreCase));
This works since Equals()
won't throw a null-reference exception when either one of the arguments is null
. StringComparison.OrdinalIgnoreCase
indicates that the comparison is case-insensitive.