tags:

views:

1480

answers:

4

Hi,

How can I select first row in a linq select statement without using foreach?

I use foreach and then break; but it has to be a better way?

I like to get the value in first row; plus check if no row is found. My problem is to get the value without foreach statement.

What I do is to find a value in my DataTable and I know the row is uniqe; so I like to do a select and get the value; plus do a check if there is no hit to be safe... :)

This is the code I use now, see below

Thank you, Rune

var var_QUERY_linq = 
    from vm_TABLE_PK in vco_DataTable_PK.AsEnumerable()
    where vm_TABLE_PK.Field<Int32>( "MyField_Int32" ) == vmp_ROW_Counter_Int32
    select vm_TABLE_PK;

foreach ( DataRow o_DataRow in var_QUERY_linq )
{
       vmp_Return_string = o_DataRow.Field<string>( "Myfield_nvarchar" );

       break;
}
+12  A: 

Just use .FirstOrDefault()

var var_QUERY_linq = (
    from vm_TABLE_PK in vco_DataTable_PK.AsEnumerable()
    where vm_TABLE_PK.Field<Int32>( "MyField_Int32" ) == vmp_ROW_Counter_Int32
    select vm_TABLE_PK).FirstOrDefault();

if(var_QUERY_linq != null)
{
    //There is a record
}
Michael La Voie
I dont believe the null check will ever turn up anything as long as you are using FirstOrDefault. I believe what you want is .First() instead.
Corey Sunwold
.First() throws and exception when nothing is returned, while FirstOrDefault() returns null (in every situation I've seen.
David Hall
@David Hall is right. Use .First() if you are expecting that there should always be one or more records so that you will get an exception if it doesn't happen. If you are expecting the possibility of no records, use .FirstOrDefault() and check for null and handle appropriately.
Michael La Voie
Or you can use DefaultIfEmpty apart from FirstOrDefault(); otherwise as David said it'll throw exception so your application will stop working.
Braveyard
Thanks!But do I need to add using System.Linq; to get FirstOrDefault to work...strange...my other linq was working without...Thanks a LOT!
RBC
A: 

I assume MyField is the field you want to get value of

var result = vm_Table_Pk.DefaultIfEmpty().First().MyField;

You can also set the default result other than NULL. Like :

var result = vm_Table_Pk.DefaultIfEmpty("Missing").First().MyField;

And don't worry Intellisense will help you along this way.

Braveyard
NICE!How Do I get my value without foreach:vmp_Return_string = o_DataRow.Field<string>( "Myfield_nvarchar" );
RBC
@RBC, I changed the code according to your question.
Braveyard
A: 

I expereinced an issue with the order of the item (OrderBy) when I use FirstOrDefault to find the first item from the list.

I would suggest you to use TAKE to get the first item:

var_QUERY_linq.Take(1).FirstOrDefault()

*Always use sql profiler if possible to check the query that is sent to SQL server.

How Lun
NICE!Thank You!
RBC
A: 

.First() will take just the first or .Single() will take the first and raise exception if there is more

Omu