简体   繁体   English

ASP.NET MVC 4从IQueryable获得价值

[英]ASP.NET MVC 4 get value from IQueryable

I have this variable here: var value = service.Get(UserId) this returns data from an IQueryable and has results. 我在这里有此变量: var value = service.Get(UserId)它从IQueryable返回数据并具有结果。 I am looking to grab the language from the results in the screenshot below, how would I do that? 我想从下面的屏幕快照中获取语言,我该怎么做? I tried value[0].language but that did not work :( 我尝试过value[0].language但是没有用:(

Any suggestions would be helpful: 任何的意见都将会有帮助:

在此处输入图片说明

I am looking to get the language value of "english" Please help. 我希望获得“英语”的语言价值,请帮助。

I tried: string value = service.Get(UserId).FirstOrDefault().Language; 我试过了: string value = service.Get(UserId).FirstOrDefault().Language;

I am getting an error 'System.Linq.IQueryable' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Linq.IQueryable could be found (are you missing a using directive or an assembly reference?)' 我收到错误消息“ System.Linq.IQueryable”不包含“ FirstOrDefault”的定义,并且找不到扩展方法“ FirstOrDefault”接受类型为“ System.Linq.IQueryable”的第一个参数(您是否缺少using指令还是装配参考?)'

Here is what I am using: 这是我正在使用的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using CTHRC.Roti.Domain.Model;
using CTHRC.Roti.Domain.Api.Services;
using CTHRC.Roti.Domain.Data.Repositories;
using CTHRC.Roti.Data.EntityFramework.Repositories;
using CTHRC.Roti.Data.EntityFramework;

IQueryables are slightly different than plain arrays. IQueryables与纯数组略有不同。 In order to see the results view, you had to enumerate the IQueryable. 为了查看结果视图,您必须枚举IQueryable。 You can actually see that in your screen shot where it says "Expanding the Results View will enumerate the IEnumerable". 您实际上可以在屏幕截图中看到“扩展结果视图将枚举IEnumerable”的截图。 This quote is the key to this scenario. 此报价是此方案的关键。 If the IEnumerable is not enumerated, then no actual data is brought from the database. 如果未枚举IEnumerable,则不会从数据库中获取任何实际数据。 There are various ways in code to enumerate the IEnumerable such as using .ToList, .Select, .Single, .First, and a few others. 代码中有多种枚举IEnumerable的方法,例如使用.ToList,.Select,.Single,.First和其他一些方法。

I believe you will be interesting in using .FirstOrDefault() . 我相信您将对使用.FirstOrDefault()感兴趣。 The "OrDefault" part just means if the list is empty no exception is thrown. “ OrDefault”部分仅表示如果列表为空,则不会引发任何异常。 This may or may not be desirable depending on the scenario. 根据情况,这可能是理想的,也可能不是理想的。

string language = service.Get(UserId).FirstOrDefault().Language;

in order to avoid an exception if there are no users returned, a projection could be first used. 为了避免在没有返回用户的情况下出现异常,可以首先使用投影。

string language = service.Get(UserId).Select(u => u.Language).FirstOrDefault();

which will produce null if there were no users. 如果没有用户,它将产生null

尝试如下所示的Cast()

 string Language= service.Get(UserId).Cast<Users>().FirstOrDefault().Language;

Several options, in your code value is a list of items not a value. 您的代码值中的几个选项是项目列表,而不是值。

string xyz = value[0].Language;

or 要么

Just get a single item

var item = service.Get(UserId).FirstOrDefault();
string xyz = item.Language;

if not strongly typed string xyz = item["Language"]; 如果不是强类型字符串xyz = item [“ Language”];

I concur with sywester's answer but with a little tweak. 我同意sywester的回答,但稍作调整。 You have to check for null first or you'll introduce a null bug into your system. 您必须先检查null,否则将在系统中引入null错误。 So 所以

var a = service.Get(UserId).FirstOrDefault(); string Language = a!=null? a.Language : string.Empty;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM