tags:

views:

64

answers:

2

Want to extract the text value from a lookup table's column in a db. EL is the entity for my db. Current code :

var QTypes = EL.ElogQueryType.Where<ElogQueryType>( eqt=> eqt.ID == queryTypeID);                       
string qType = QTypes.First().QueryType; 

I get a list when I just pull .Select(... so something is wrong.

+2  A: 

It's not clear what's wrong, as your current query should give you what you're after. However, you can also use the overload of First which takes a predicate:

string qType = EL.ElogQueryType.First(eqt => eqt.ID == queryTypeID)
                               .QueryType;

You say you "get a list when [you] pull .Select(" but it's not really clear what you mean. You haven't said what's wrong with the code you've already specified.

(As Kelsey says, there are alternatives to First: FirstOrDefault, SingleOrDefault, Single and even Last should you wish.)

Jon Skeet
+1  A: 

You should be able to just do if you know you will be just getting one item:

var QTypes = EL.ElogQueryType.Where(eqt=> eqt.ID == queryTypeID).Single().QueryType;

If you are not sure if you will get one or nothing use SingleOrDefault().

If you just want the first since you are expecting many records do:

var QTypes = EL.ElogQueryType.First(eqt=> eqt.ID == queryTypeID).QueryType;

Same applies if you don't know if you will get anything, use FirstOrDefault.

Kelsey