Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
3.80/5 (5 votes)
See more:
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..
Posted
Updated 3-Aug-12 9:38am
v3
Comments
Kenneth Haugland 3-Aug-12 15:28pm    
I dont understand your question. Do you want to have a new roe pre-filled with information?
Gabrijel Baban 3-Aug-12 15:37pm    
Explanation edited..
Kenneth Haugland 3-Aug-12 15:42pm    
IUll give you a 5 for the improved question :) (its rare to see)
Sergey Alexandrovich Kryukov 3-Aug-12 15:38pm    
I think I understand this (incomplete of course) question well enough to answer, please see.
--SA
Aditya Mangipudi 3-Aug-12 15:29pm    
I dont understand the desired text part.

Okay, I get it :D

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

C#
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..

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



:) Here it is and it's working :)
 
Share this answer
 
v2
Comments
Aditya Mangipudi 3-Aug-12 16:12pm    
Good Job.
This method (http://msdn.microsoft.com/en-us/library/bb340482.aspx[^]) is too trivial to discuss it seriously:
C#
if (MyCollection.Count < 1)
   WriteMyDesiredTextAsDataInThatRow(); // :-)
else
   WriteInThatRow(MyCollection[0]);


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;
 
Share this answer
 
Comments
Gabrijel Baban 3-Aug-12 15:48pm    
I don't need to insert anything to my DB.. What I really do is this: I'm taking data from DB and show it in labels that I created in windowsForm.. So, If there is data in DB my labels are filled with it, but when there is no data I'm getting error. So I want to write in that label some message to replace non existing data and continue with code execution. YOur solution is for inserting data in DB, am I right?
Sergey Alexandrovich Kryukov 3-Aug-12 17:00pm    
I am not talking about insert. I answer your question about FirstOfDefault. Anything not clear?
--SA
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
 
Share this answer
 
I dont think FirstOrDefault would help in this case. If i understood correctly, then you can just hardcode it your ELSE part or DEFAULT (is using switch case). Try using Enumeration for these.

Good Luck!!
 
Share this answer
 
Comments
Gabrijel Baban 3-Aug-12 15:48pm    
Not really know how to use it..
Is there way to do something like this:


xx.First();
IF(xx ) -- what condition? How to check if it's have data or not?
label.Text=xx;
ELSE
label.Text="MY TEXT";

Or in some way to do with FirstOrDefault
Aditya Mangipudi 3-Aug-12 15:52pm    
As Sergey posted above you can do something like this.

if (MyCollection.Count < 1)
MYENUMERATION.NONAME
else
WriteInThatRow(MyCollection[i]);

where [i] is row. Again this is just a algorithm. Try to code it yourself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900