使用FirstOrDefault()方法 [英] Use of FirstOrDefault() method

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

问题描述

大家好,这是我的问题.

当我使用FirstOrDefault方法时,当行上没有数据时,FirstOrDefault方法可以将所需的文本写为该行中的数据吗?

对不起,不好的解释..

好的,这更好:

例如,我的表TEST_TABLE
中有此数据 1.第一人称
2.第二人称
3.第三人称

当我通过Querry从THIS_TABLE获取内容时,如果我的过滤器1是第一人称,如果我的过滤器是2我将获得第二人称,如果3我将得到第三人称..
但是当我的过滤器达到4时,我得到一个错误,提示那里什么都没有". 如此,当行中没有数据时,我要在该字段"NO NAME"或其他内容上写..
像这样的东西.FirstOrDefault(如果没有数据,则为默认文本"); ?!

我认为现在更容易理解了..

Hello to everyone and here is my question.

Is there way when I''m using FirstOrDefault method, when there is no data on row, that FirstOrDefault method write my desired text as data in that row?

Sorry for bad explanation..

Okay here it is better:

I have for example this data in my table TEST_TABLE
1. First Person
2. Second Person
3. Third Person

When I''m getting stuff from THIS_TABLE with querry, if is my filter 1 i''ll get First Person, if my filter is 2 I''ll get Second Person, if 3 i''ll get Third Person..
But when my filter reaches 4 I get error that says "nothing there"..
Sooo, when there is no data in row I want to write on that field "NO NAME" or something else..
SOmething like this .FirstOrDefault("THIS IS DEFAULT TEXT IF THERE IS NO DATA"); ?!

I think that now is much clearer to understand..

推荐答案

好吧,我明白了:D

创建linq Querry时要做的第一件事是使用.FirstOrDefault()方法

Okay, I get it :D

First thing to do when creating linq querry is to use .FirstOrDefault() method

var xx=(from x in y 
        where id=TempID
        select z).FirstOrDefault();



第二件事是按照我所说的进行检查..



Second thing to do is to check that with if else as I said..

if (xx == null)
  label = "No data";
else 
  label = xx;




:)它就在这里并且正在工作:)




:) Here it is and it''s working :)


此方法(
This method (http://msdn.microsoft.com/en-us/library/bb340482.aspx[^]) is too trivial to discuss it seriously:
if (MyCollection.Count < 1)
   WriteMyDesiredTextAsDataInThatRow(); // :-)
else
   WriteInThatRow(MyCollection[0]);



所需文本"或在该行中写入"的含义完全由您决定,但是如果源为空,则Enumerable.FirstOrDefault<tsource></tsource>返回default(TSource),在您的情况下它可能是也可能不是您的所需文本".

—SA;



What you mean by "desired text" or "write in that row" is totally up to you, but Enumerable.FirstOrDefault<tsource></tsource> returns default(TSource) in case of empty source, which may or may not be your "desired text" in your case.

—SA;


最重要的是您需要了解的FirstOrDefault First 信息,如果First找不到值,您将获得一个例外.

在大多数情况下,如果没有匹配项,FirstOrDefault 将返回null ,因此您不必处理异常.要确定是否未找到该值,就是要检查该值是否为空. Value 类型没有空值.如果您正在执行返回数字的Linq查询,则找不到任何内容,则返回值将为零.如果不是值类型,则可能会调用FirstOrDefault FirstOrNullIfNoResults
What you need to know about FirstOrDefault and First is most significanly, if First does not find a value, you will get an exception.

In most cases FirstOrDefault will return null if there is no match, thus you do not have to deal with an exception. To find out if the value was not found is to check if the value is null. Value types do not have a null value. If you are doing a Linq query that returns a number and there is nothing found, then the returned value will be zero. If it was not for Value types, then could probably call FirstOrDefault FirstOrNullIfNoResults


这篇关于使用FirstOrDefault()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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