I have a query with single result. The result is an anonymouse type. How can I use access the type so I don’t have to use query.Single() every time?
This is my query:
var result = from row in db.Table
select new { CustomName = row.RowName };
This is how I use it right now:
string name = result.Single().CustomName;
Of course my real code has a lot more properties and for every property I have to call .Single() every time. Is there an easier way to access CustomName here?
Have you tried assigning the
Singleresult to a variable?Furthermore, each time you call
Single()it will execute the query. You should grab the value once and use it wherever you need. As long as you usevaryou should be fine, you just can’t return that anonymous type from a method.