结构集合的FirstOrDefault()结果? [英] FirstOrDefault() result of a struct collection?

查看:335
本文介绍了结构集合的FirstOrDefault()结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 struct 的集合(它实际上是一个WCF datacontract,但我假设这里没有轴承。)

 列表< OptionalExtra> OptionalExtras; 

OptionalExtra struct

  public partial struct OptionalExtra 
/ pre>

现在我运行下面的语句:

  OptionalExtra multiOptExtra = OptionalExtras.Where(w => w.Code == optExtra.Code).FirstOrDefault(); 
if(multiOptExtra!= null)
{

}


$ b b

现在这不会编译:


操作符!=不能应用于OptionalExtra类型的opperands
'< null>'


OptionalExtra 是一个 struct 。除非定义为可空类型,否则我相信是不可空的?



所以我的问题是,如果我的其中语句返回没有结果什么将是 FirstOrDefault 调用的结果?它会抛出异常吗?



偶尔,这应该永远不会发生,但更安全比对不起。

解决方案

如果您的集合为空, FirstOrDefault 将返回 default(OptionalExtras)。结构的默认值是结构的所有值依次默认初始化(即零,null等)。



如果你假设会有一个元素,而您的代码不能使用空集合,使用 First()而不是,因为这将在您的集合为空时抛出异常中。



如果你不能假设会有一个元素,但也不能处理struct default初始化,你可以struct可空,例如如下:

  OptionalExtras 
.Where(w => w.Code == optExtra.Code)
.Cast< OptionalExtras?>()
.FirstOrDefault();

这样,即使对于struct,也可以获得null返回。这里的关键思想是扩展可能值的集合,以包含 OptionalExtras 以外的其他东西,以允许检测空列表。如果你不喜欢nullables,你可以使用 Maybe<> 实现(不是.NET内建),或者使用空或单例列表 .Take(1).ToArray()但是,一个可空的结构可能是你最好的选择。



TL ; DR;




  • .FirstOrDefault< T>()返回 default(T)如果序列为空

  • 使用 .First()假设列表不为空。

  • 当您无法假定为非空时,可以转为可空,然后使用 .FirstOrDefault< T>()列表不为空。


So I've got a collection of structs (it's actually a WCF datacontract but I'm presuming this has no bearing here).

List<OptionalExtra> OptionalExtras;

OptionalExtra is a struct.

public partial struct OptionalExtra

Now I'm running the below statement:

OptionalExtra multiOptExtra = OptionalExtras.Where(w => w.Code == optExtra.Code).FirstOrDefault();
if (multiOptExtra != null)
{

}

Now this won't compile:

the operator != cannot be applied to opperands of type OptionalExtra and '<null>'

After a little googling I realised it's because OptionalExtra is a struct. Which I believe is not nullable unless defined as a nullable type?

So my question is, if my where statement returns no results what will be the outcome of the FirstOrDefault call? Will it thrown an exception?

Incidently this should never happen but better safe than sorry.

解决方案

If your collection is empty, FirstOrDefault will return default(OptionalExtras). The default value of a struct is the struct with all its values in turn default initialized (i.e. zero, null, etc.).

If you assume that there will be an element and your code doesn't work with an empty collection, Use First() instead, since that will throw an exception when your collection is empty. It's generally better to fail fast than to return wrong data.

If you cannot assume that there will be an element, but also cannot deal with struct default initialization, you might make the struct nullable, for example as follows:

OptionalExtras
    .Where(w => w.Code == optExtra.Code)
    .Cast<OptionalExtras?>()
    .FirstOrDefault();

This way you can get a null return even for a struct. The key idea here is to extend the set of possible values to include something other than an OptionalExtras to allow detection of an empty list. If you don't like nullables, you could instead use a Maybe<> implementation (not a .NET builtin), or use an empty-or-singleton list (e.g. .Take(1).ToArray(). However, a nullable struct is likely your best bet.

TL;DR;

  • .FirstOrDefault<T>() returns default(T) if the sequence is empty
  • Use .First() instead if you assume the list is non-empty.
  • Cast to nullable and then use .FirstOrDefault<T>() when you cannot assume the list is non-empty.

这篇关于结构集合的FirstOrDefault()结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆