如何使用Where扩展函数在N个列表中选择一个元素?

发布于 2024-09-19 11:25:35 字数 287 浏览 7 评论 0原文

假设我有一个类 AddressType 定义如下:

public class AddressType {
    public int AddressTypeId { get; set; }
    public string Description { get; set; }
}

在代码中有一个 List 对象,如何选择具有已知 AddressTypeId 属性的 AddressType 对象?

我从未使用过 List.Where 扩展功能...

谢谢!

Suppose I have a class AddressType defined as is:

public class AddressType {
    public int AddressTypeId { get; set; }
    public string Description { get; set; }
}

Having a List object in code, how do I select an AddressType object with a known AddressTypeId property?

I have never used the List.Where extension function....

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

徒留西风 2024-09-26 11:25:35

您可以使用 Where 获取列表中具有特定 ID 的所有 AddressType 对象:

IEnumerable<AddressType> addressTypes = list.Where(a => a.AddressTypeId == 123);

但如果您只想要唯一一个具有特定 ID 的 AddressType 对象,您可以使用First的特定ID:

AddressType addressType = list.First(a => a.AddressTypeId == 123);

这将在列表中找到ID为123的第一个AddressType,如果没有找到,将抛出异常。

另一种变体是使用 FirstOrDefault

AddressType addressType = list.FirstOrDefault(a => a.AddressTypeId == 123);

如果不存在具有所请求 ID 的 AddressType,它将返回 null

如果您想确保列表中恰好存在一个具有所需 ID 的 AddressType,您可以使用 Single

AddressType addressType = list.Single(a => a.AddressTypeId == 123);

这将引发异常,除非恰好存在一个 ID 为 123 的列表中的 AddressTypeSingle 必须枚举整个列表,使其比 First 慢。

You can get all AddressType objects in the list having a specific ID by using Where:

IEnumerable<AddressType> addressTypes = list.Where(a => a.AddressTypeId == 123);

But if you only want the one and only AddressType having a specific ID you can use First:

AddressType addressType = list.First(a => a.AddressTypeId == 123);

This will find the first AddressType in the list having ID 123 and will throw an exception if none is found.

Another variation is to use FirstOrDefault:

AddressType addressType = list.FirstOrDefault(a => a.AddressTypeId == 123);

It will return null if no AddressType having the requested ID exists.

If you want to make sure that exactly one AddressType exists in the list having the desired ID you can use Single:

AddressType addressType = list.Single(a => a.AddressTypeId == 123);

This will throw an exception unless there is exactly one AddressType in the list having ID 123. Single has to enumerate the entire list making it a slower than First.

望她远 2024-09-26 11:25:35
IEnumerable<AddressType> addressList = ...
IEnumerable<AddressType> addresses = addressList.Where(a => a.AddressTypeId == 5);

或者:

IEnumerable<AddressType> addresses = 
    from address in addressList
    where address.AddressTypeId == 5
    select address;
IEnumerable<AddressType> addressList = ...
IEnumerable<AddressType> addresses = addressList.Where(a => a.AddressTypeId == 5);

or:

IEnumerable<AddressType> addresses = 
    from address in addressList
    where address.AddressTypeId == 5
    select address;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文