在单个 LINQ 表达式中嵌入 null 测试
让我们从一个简单的示例类开始:
public class Foo
{
public DateTime Date { get; set; }
public decimal Price { get; set; }
}
然后创建一个列表:
List<Foo> foos = new List<Foo>;
我想根据日期返回列表中一项的格式化价格或“N/A”,所以例如我可以写:
Foo foo = foos.FirstOrDefault(f => f.Date == DateTime.Today);
string s = (foo != null) ? foo.Price.ToString("0.00") : "N/A";
我会喜欢将上面两行组合起来像以下内容:
string s = foos.FirstOrDefault(f => f.Date == DateTime.Today).Price.ToString("0.00") ?? "N/A";
但是,这并没有达到我想要的效果,因为 if (f => f.Date == DateTime.Today)
不返回 Foo 然后返回抛出NullReferenceException
。
因此,是否可以使用 LINQ 创建 1 个语句来返回格式化价格或“N/A”?
Let's start with a simple example class:
public class Foo
{
public DateTime Date { get; set; }
public decimal Price { get; set; }
}
Then create a list:
List<Foo> foos = new List<Foo>;
I would like to return a formatted price or "N/A" of one item in the list based on a date, so for example I could write:
Foo foo = foos.FirstOrDefault(f => f.Date == DateTime.Today);
string s = (foo != null) ? foo.Price.ToString("0.00") : "N/A";
I would like to combine the above 2 lines like the following:
string s = foos.FirstOrDefault(f => f.Date == DateTime.Today).Price.ToString("0.00") ?? "N/A";
However, this does not achieve what I want because if (f => f.Date == DateTime.Today)
does not return a Foo then a NullReferenceException
is thrown.
Therefore, is it possible with LINQ to create just 1 statement to either return the formatted price or "N/A"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果先过滤然后选择,则可以使用空合并运算符 (
??
),如下所示:If you filter first and then select, you can use the null coalescing operator (
??
) like so:一种方法是在调用 ToString 之前简单地检查 FirstOrDefault 的结果是否为 null:
另一种方法是为合并运算符创建一个扩展方法,该方法也接受投影委托,类似:
然后这样调用它:
One way would be to simply check if result of
FirstOrDefault
is null, before callingToString
:Another way would be to create an extension method for a coalescing operator which also accepts a projection delegate, something like:
And then call it like this:
string s = foos.Where(f => f.Date == DateTime.Today).Select(f => f.Price.ToString("0.00")).FirstOrDefault();
string s = foos.Where(f => f.Date == DateTime.Today).Select(f => f.Price.ToString("0.00")).FirstOrDefault();