如何在 linq 中编写 IsNull()?

how to write IsNull() in linq?

我想写下面的查询 linq.please 帮帮我。

SELECT ISNULL(userfirstname, '')userfirstname, useremail, usermobile FROM [user] 
WHERE useremail='eeeee@gmail.com'
db.user.Where(u => u.useremail == "eeeee@gmail.com").
        Select(u => u.userfirstname ?? string.Empty);

这将针对您指定的电子邮件进行过滤,然后通过返回 userfirtname 或空字符串(如果 userfirstnamenull)来投影结果。


对于您更新的问题:

db.user.Where(u => u.useremail == "eeeee@gmail.com").
        Select(u => new { 
             userfirstname = u.userfirstname ?? string.Empty,
             useremail = u.useremail,
             usermobile = u.usermobile});

此 returns 包含您的值的匿名类型的实例序列。如果需要,您也可以为此创建一个数据 class:

public class MyUser
{
    public string userfirstname { get; set; }
    public string useremail { get; set; }
    public string usermobile { get; set; }
}

db.user.Where(u => u.useremail == "eeeee@gmail.com").
        Select(u => new MyUser { 
             userfirstname = u.userfirstname ?? string.Empty,
             useremail = u.useremail,
             usermobile = u.usermobile});
user.Where(x => x.useremail == "eeeee@gmail.com").Select(x => new {x.userfirstname ?? string.Empty, x.userlastname, x.useremail, x.usermobile})

你最后生成了一个匿名类型