SUN Jiangong
SUN Jiangong

Reputation: 5312

How to get the first element of LinqToEntities result?

I want to get the first element of the LinqToEntities request.

Here is my code:

var para = (from param in context.Parameters
            where param.Code == paramCode
            select param.ValueDecimal);

Does anyone know how to do that?

Thanks

**Edit:** 

Thanks for your responses. It works.

Upvotes: 2

Views: 76

Answers (2)

Oded
Oded

Reputation: 498904

Use the First or FirstOrDefault methods:

var para = (from param in context.Parameters
            where param.Code == paramCode
            select param.ValueDecimal).First();

First will throw an exception if the Enumerable is empty.

var para = (from param in context.Parameters
            where param.Code == paramCode
            select param.ValueDecimal).FirstOrDefault();

Upvotes: 4

Phil Hunt
Phil Hunt

Reputation: 8521

FirstOrDefault returns the first element in the list or null if the list has no elements.

var para = (from param in context.Parameters
            where param.Code == paramCode
            select param.ValueDecimal).FirstOrDefault();

Upvotes: 4

Related Questions