我想在linq.please帮助我写下面的查询。
SELECT ISNULL(userfirstname, '')userfirstname, useremail, usermobile FROM [user]
WHERE useremail='eeeee@gmail.com'
答案 0 :(得分:2)
db.user.Where(u => u.useremail == "eeeee@gmail.com").
Select(u => u.userfirstname ?? string.Empty);
这会过滤指定的电子邮件,然后在userfirtname
为userfirstname
时返回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})
你在最后生成一个匿名类型