LINQ SELECT FIRST ROW

Viewed 68299

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;
}
4 Answers
Related