在 LINQ toEntity 表达式中,如下所示:
var vote = (from vote in db.Vote where
vote.Voter.Id == user.Id
select v).FirstOrDefault();
如何添加 DefaultIfEmpty 值,以便在没有投票时我可以获得默认值?
最佳答案
如果 Vote
是引用类型并因此使用 null 作为其默认值,另一种方法是使用 null 合并运算符:
var vote = (db.Vote
.Where(v => v.Voter.Id == user.Id)
.FirstOrDefault()) ?? defaultVote;
关于linq - 如果 LINQ toEntity 查询不返回任何值,如何返回默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1127054/