你可以在MVC中的FirstOrDefault之后使用Where子句吗? [英] Can you use the Where clause after FirstOrDefault in MVC?

查看:68
本文介绍了你可以在MVC中的FirstOrDefault之后使用Where子句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开发一个MVC 3项目..只是在这里和那里更新一些东西..



是否可以使用FirstOrDefault后跟Where子句?此刻它不让我。有没有可能解决这个问题?



示例......



<前lang = c#> var car = carContext.Vehicles.FirstOrDefault(x => x.Reg = reg)。其中(x => x.manufacturere == < span class =code-string> BMW





有没有办法解决这个问题?

解决方案

 var car = carContext.Vehicles.FirstOrDefault(x = > x.Reg == reg&& x.manufacturere ==BMW)





这将选择第一个注册表和制造商匹配提供的参数。


否。

FirstOrDefault返回单个值,而不是集合 - 所以Where(仅对集合进行操作)具有什么都没用。

您可以反过来使用它: Where(...)。FirstOrDefault()


Hi All,
I am working on an MVC 3 project .. just updating a few things here and there..

Is it possible to Use FirstOrDefault followed by Where clause? at the moment it does not let me. Is there any possible solution to go around this?

Example ...

var car = carContext.Vehicles.FirstOrDefault(x =>x.Reg = reg).Where(x =>x.manufacturere == "BMW")



is there a way around this??

解决方案

var car = carContext.Vehicles.FirstOrDefault(x =>x.Reg == reg && x.manufacturere == "BMW")



That will select the first where the reg and manufacturer match the parameters supplied.


No.
FirstOrDefault returns a single value, not a collection - so Where (which operates only on collections) has nothing to work with.
You can use it the other way round though: Where(...).FirstOrDefault()


这篇关于你可以在MVC中的FirstOrDefault之后使用Where子句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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