Home » .Net Framework

Selecting a single value from a returned row

I hope this is the appropriate forum, but I have been struggling with this for the past few days. How do you query a single value in a one row result from an Entity query? 

What I have is about 24 columns that are in the table, I run a query, it always returns only one row, but I have to dynamically choose which column is selected as the result in that row. How can I accomplish this?


3 Answers Found


Answer 1

I have this in my code:


Dim customQuery As ObjectQuery(Of tblSubtestNorm) = From a In db.tblSubtestNorms Where a.ST = tempvalue2 _
                  And a.RS = tempvalue Select a


The above code always returns  a single  row of data. The question is, how do I then dynamically  state that I want to return the value in "X" column? The column  I am returning is always going to be different based on property settings elsewhere.



Answer 2

Hi MarkSA,

If you want to select particular columns, you can use projections in your query and shape your results by picking particular properties rather than entities. If the column  you are returning is always different and you want to choose  the column dynamically, you can use projecting with query  builder methods, something like :

Dim context AsNew NorthwindEntities
Dim colSel AsString = "CompanyName"'Column name you want to select. You can change the name to othersDim customQuery = context.Customers.Where("it.CustomerID = 'VINET'").Select("it." & colSel)

ForEach mm In customQuery

And I think you can also achieve what you want using Dynamic LINQ.

By the way, I am moving this thread to "ADO.NET Entity Framework and LINQ to Entities" forum  for better response.

Best regards,

Alex Liang

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


Answer 3

That worked beautifully. Thank you so much for your help on this Alex.


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure