如何在linq中编写IsNull()?

时间:2016-05-19 05:09:49

标签: sql linq isnull

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

SELECT ISNULL(userfirstname, '')userfirstname, useremail, usermobile FROM [user] 
WHERE useremail='eeeee@gmail.com'

2 个答案:

答案 0 :(得分:2)

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

这会过滤指定的电子邮件,然后在userfirtnameuserfirstname时返回null或空字符串来投射结果。

有关更新的问题:

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

这将返回包含您的值的匿名类型的实例序列。如果需要,您也可以为此创建数据类:

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});

答案 1 :(得分:1)

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

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