相当于Java WHERE子句在C#中的LINQ [英] Java equivalent of Where Clause in C# Linq

查看:539
本文介绍了相当于Java WHERE子句在C#中的LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在C#中做到这一点:

I can do this in C# :

int CheetahNumber = 77;
Animal Cheetah = Model.Animals.Where(e => e.AnimalNo.Equals(CheetahNumber)).FirstOrDefault();

例如在Java中我有的ArrayList<动物>动物

For example in Java I have ArrayList<Animal> Animals

我怎样才能查询这样一个ArrayList?谢谢你。

How can I query such an ArrayList? Thanks.

推荐答案

的Java 8推出的流API 允许类似结构的那些Linq中。

Java 8 introduces the Stream API that allows similar constructs to those in Linq.

您例如查询,可以表示为:

Your query for example, could be expressed:

int cheetahNumber = 77;

Animal cheetah = animals.stream()
  .filter((animal) -> animal.getNumber() == cheetahNumber)
  .findFirst()
  .orElse(Animal.DEFAULT);

您显然需要锻炼,如果存在缺省值,这似乎很奇怪,在这种情况下,但我'它已经因为这是你的问题的代码不显示。

You'll obviously need to workout if a default exists, which seems odd in this case, but I've shown it because that's what the code in your question does.

这篇关于相当于Java WHERE子句在C#中的LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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