开发者

C# lambda extract a single row string value

开发者 https://www.devze.com 2023-01-08 20:07 出处:网络
Want to extract the text value from a lookup table\'s column in a db. EL is the entity for my db. Current code :

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.Elog开发者_运维百科QueryType.Where<ElogQueryType>( eqt=> eqt.ID == queryTypeID);                       
string qType = QTypes.First().QueryType; 

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


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号